error while compiling - conversion from DWORD to LPCVOID - c++

I found this code for injecting dll files and call functions from them. I got an exe and the source code. The exe is working but when i try to compile the source code i get this error:
LoadDll.cpp: In Funktion »bool ExecuteRemoteThread(HANDLE, BOOL, BOOL, wchar_t*, wchar_t*)«:
LoadDll.cpp:313:62: Fehler: ungültige Umwandlung von »DWORD (__attribute__((__stdcall__)) *)(RemoteThreadBlock*) {aka long unsigned int (__attribute__((__stdcall__)) *)(RemoteThreadBlock*)}« in »LPCVOID {aka const void*}« [-fpermissive]
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/../../../../include/windows.h:50:0,
from LoadDll.cpp:16:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/../../../../include/winbase.h:2215:24: Fehler: Argument 3 von »BOOL WriteProcessMemory(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T*)« wird initialisiert [-fpermissive]
The source code can be found at: https://pastebin.com/AuZpy57U
It seems like the error is in line 313
if ( ! WriteProcessMemory( hProcess, p, &RemoteThread, 0, 0 ) )
I hope you can help me compiling this.

if ( ! WriteProcessMemory( hProcess, p, &RemoteThread, 0, 0 ) )
You have to cast the third parameter:
if ( !WriteProcessMemory( hProcess, p, reinterpret_cast<LPCVOID>(&RemoteThread), 0, 0 ) )
Update:
To get rid of undefined reference to 'WinMain#16' you should use
int main()
{
int argc;
wchar_t **argv = CommandLineToArgvW(GetCommandLineW(), &argc);
// ...
instead of
int wmain(int argc, wchar_t *argv[])
{
// ...

Related

Unable to pass TCHAR as parameter in C/C++

Here is Minimal example :-
#include <stdio.h>
#include <Windows.h>
using namespace std;
void myFunc(TCHAR Path)
{
printf("pathLen : %lu\n", sizeof(Path));
printf("character size : %lu\n", sizeof(*Path));
printf("pathLenInBytes : %lu\n", sizeof(Path) * sizeof(*Path));
}
int main()
{
TCHAR selfPath[MAX_PATH];
if (GetModuleFileName(NULL, selfPath, MAX_PATH) == 0) // Getting exe File Location
printf("Error : %lu\n", GetLastError());
printf("Self Path : %s\n", selfPath);
myFunc(selfPath);
return 0;
}
Here is Error Output from MinGW-W64 Compiler :-
g++ -Os -s -o goga.exe tesst.cpp
tesst.cpp: In function 'void myFunc(LPCSTR, TCHAR)':
tesst.cpp:9:43: error: invalid type argument of unary '*' (have 'TCHAR' {aka 'char'})
9 | printf("character size : %lu\n", sizeof(*Path));
| ^~~~
tesst.cpp:10:35: error: 'pathLen' was not declared in this scope
10 | printf("pathLenInBytes : %lu\n", pathLen * sizeof(*Path));
| ^~~~~~~
tesst.cpp:10:53: error: invalid type argument of unary '*' (have 'TCHAR' {aka 'char'})
10 | printf("pathLenInBytes : %lu\n", pathLen * sizeof(*Path));
| ^~~~
tesst.cpp: In function 'int main()':
tesst.cpp:23:22: error: invalid conversion from 'TCHAR*' {aka 'char*'} to 'TCHAR' {aka 'char'} [-fpermissive]
23 | myFunc("AppBroker", selfPath);
| ^~~~~~~~
| |
| TCHAR* {aka char*}
tesst.cpp:6:32: note: initializing argument 2 of 'void myFunc(LPCSTR, TCHAR)'
6 | void myFunc(LPCSTR Name, TCHAR Path)
| ~~~~~~^~~~
But If I put the GetModuleFineName() directy inside myFunc() then it works :-
#include <stdio.h>
#include <Windows.h>
using namespace std;
void myFunc()
{
TCHAR selfPath[MAX_PATH];
if (GetModuleFileName(NULL, selfPath, MAX_PATH) == 0) // Getting exe File Location
printf("Error : %lu\n", GetLastError());
printf("Self Path : %s\n", selfPath);
printf("pathLen : %lu\n", sizeof(selfPath));
printf("character size : %lu\n", sizeof(*selfPath));
printf("pathLenInBytes : %lu\n", sizeof(selfPath) * sizeof(*selfPath));
}
int main()
{
myFunc();
return 0;
}
But I dont need it this way. How can i solve this error ?
EDIT : Tried replacing myFunc(TCHAR Path) with myFunc(TCHAR *Path) & also with myFunc(TCHAR Path[]). Both Work and program compiles successfully but the output is different that expected output now !
Expected Output :-
Self Path : C:\Users\username\Desktop\Coding\PETS\muse\goga.exe
pathLen : 260
character size : 1
pathLenInBytes : 260
Output that I Get:-
Self Path : C:\Users\username\Desktop\Coding\PETS\muse\goga.exe
pathLen : 8
character size : 1
pathLenInBytes : 8
I make an attempt to answer
In your first version, your prototype must be
myFunc(TCHAR *Path) or myFunc(TCHAR Path[]) because a path is an array of TCHAR, thus a TCHAR*
(a starting documentation can be found here or here)
What you obtained from the first code that compiles is only what you have asked.
Let us see:
printf("pathLen : %lu\n", sizeof(Path));
printf("character size : %lu\n", sizeof(*Path));
printf("pathLenInBytes : %lu\n", sizeof(Path) * sizeof(*Path));
First remark: you should not use sizeof (thanks # Remy LEABEAU for review) with TCHAR* but _tcslen() or lstrlen
In the first line, you asked to display the size of path which is a pointer (a TCHAR*). The size
of a pointer can be 4 bytes or 8 bytes depending on your system(ref). So 8 is correct.
In an array, its name is also the adress of the first element in it. Thus if you try to printf
sizeof(*Path), you ask to print the size of the first character pointed by the pointer, thus 1.
The two previous lines also explain what the thrid line gives you: 1*8 = 8.
If pathLenis the size in byte of the path, you may use _tcslen() or lstrlen() to compute the length of the path and then use sizeof(TCHAR) as found here
Proposition to obtain what your needed output:
printf("pathLen : %lu\n", _tcslen(Path));
printf("TCHAR size : %lu\n", sizeof(TCHAR));
printf("pathLenInBytes : %lu\n", _tcslen(Path)* sizeof(*Path));

Assertion failed DirectX 11

I'm working on a DirectX 11 project and keep getting this error:
It says that error occurs in model.cpp file which my teacher sent me. Line 400: assert(file);
void CModel::LoadMaterial( const char *FileName, MODEL_MATERIAL **MaterialArray, unsigned int *MaterialNum )
{
char dir[MAX_PATH];
strcpy(dir, FileName);
PathRemoveFileSpec(dir);
char str[256];
FILE *file;
file = fopen( FileName, "rt" );
assert(file);
MODEL_MATERIAL *materialArray;
unsigned int materialNum = 0;
Or maybe I should send the whole model.cpp file to see why there is that Runtime Error?
Ensure that the FileName file exists. The assert is being thrown because the file object is null (0) as the call to fopen failed.

SHFileOperation error 87

I am trying to copy the contents of the A drive into folder C:\test\disk1. Folder disk1 already exists. The program compiles but when it runs I get error 87. I know error 87 has something to do with an invalid parameter but Im not sure where the problem lies. Has anyone any ideas?
#include <Windows.h>
#include <stdio.h>
int main(int argc, char ** argv)
{
const wchar_t *const sourceFile = L"A:\\";
const wchar_t *const outputFile = L"C:\\test\\disk1";
SHFILEOPSTRUCTW fileOperation;
memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW));
fileOperation.wFunc = FO_COPY;
fileOperation.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR |
FOF_NOERRORUI | FOF_FILESONLY;
fileOperation.pFrom = sourceFile;
fileOperation.pTo = outputFile;
int result = SHFileOperationW(&fileOperation);
if (result != 0)
{
printf("SHFileOperation Failure: Error%u\n", result);
return 1;
}
memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW));
printf("OK\n");
return 0;
}
Note the documentation of SHFILEOPSTRUCT and in particular that of pFrom and pTo:
PCZZTSTR pFrom;
PCZZTSTR pTo;
What does PCZZTSTR mean?
pFrom
Type: PCZZTSTR
Note This string must be double-null terminated.
So your fix is to supply an additional trailing zero.
const wchar_t *const sourceFile = L"A:\\\0";
const wchar_t *const outputFile = L"C:\\test\\disk1\0";
Note that Windows API functions accept / as a directory separator, so that can be written as the slightly easier to read:
const wchar_t *const sourceFile = L"A:/\0";
const wchar_t *const outputFile = L"C:/test/disk1\0";
(PCZZSTR is actually a pointer to a list of zero terminated strings which is terminated by an empty string.)

Invalid conversion from HANDLE to HINSTANCE (Getting a kernel function's address)

I'm trying to find the address of the SetProcessDEPPolicy function of the Windows API in kernel32 (see my problem here and the first answer I got).
I've never written a Windows C++ program before so I'm a bit lost but I have this so far:
#include <windows.h>
#include <iostream>
int main(int argc, char* argv[])
{
HANDLE kernel32 = GetModuleHandle("kernel32");
FARPROC* funcAddr = (FARPROC *) GetProcAddress(kernel32, "SetProcessDEPPolicy");
std::cout << "# ";
}
I'm getting the following error on line 7:
C:\Documents and Settings\John\Desktop>c++ finddep.cpp -o finddep.exe finddep.cpp: In function 'int main(int, char**)': finddep.cpp:7:79: error: invalid conversion from 'HANDLE {aka void*}' to 'HINSTA NCE' [-fpermissive] FARPROC funcAddr = (FARPROC *) GetProcAddress(kernel32, "SetProcessDEPPolicy") ;
^
In file included from c:\mingw\include\windows.h:50:0,
from finddep.cpp:1: c:\mingw\include\winbase.h:1675:27: error: initializing argument 1 of 'int (__ attribute__((__stdcall__)) * GetProcAddress(HINSTANCE, LPCSTR))()' [-fpermissive ] WINBASEAPI FARPROC WINAPI GetProcAddress(HINSTANCE,LPCSTR);
^ finddep.cpp:7:79: error: cannot convert 'int (__attribute__((__stdcall__)) **)() ' to 'FARPROC {aka int (__attribute__((__stdcall__)) *)()}' in initialization FARPROC funcAddr = (FARPROC *) GetProcAddress(kernel32, "SetProcessDEPPolicy") ;
I couldn't find any good ideas on how to solve this from Google.
(Once I get this to compile, how can I print the address in the pointer?)
EDIT: Added Cyclone's suggestion from the comment, getting same error Invalid conversion from HANDLE to HINSTANCE
This is how you should do it:
#include <windows.h>
#include <iostream>
int main(int argc, char* argv[])
{
HMODULE kernel32 = GetModuleHandleA("kernel32");
FARPROC *funcAddr = (FARPROC *)GetProcAddress(kernel32, "SetProcessDEPPolicy");
std::cout << "#" << funcAddr;
}

error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'const wchar_t [58]' to 'LPCSTR'

guys whay i have this to error and how i can fix that?!
but before that i say i searched in the net and find a solution for my problem such as:
the properties, and navigate to Configuration Properties > General. Switch Character Set to "Use Multi-Byte Character Set".
but also i have same error?!!
The error's:
Error 7 error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'const wchar_t [58]' to 'LPCSTR'
Error 8 error C2664: 'int std::basic_string<_Elem,_Traits,_Ax>::compare(const std::basic_string<_Elem,_Traits,_Ax> &) const' : cannot convert parameter 1 from 'CHAR [260]' to 'const std::basic_string<_Elem,_Traits,_Ax> &'
and my code is:
#include "StdInc.h"
#include<fstream>
#include<sstream>
#include<string>
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<Windows.h>
#include<TlHelp32.h>
using std::ifstream;
using std::string;
using std::getline;
using std::ios;
using std::cerr;
using std::cout;
using std::endl;
using std::fixed;
using std::left;
using std::right;
using std::showpoint;
using std::cin;
class check {
public :
void check_seta () {
ifstream cfgm2("finfin.cfg",ios::in);
string cfgLine;
while (getline(cfgm2,cfgLine)) {
if (string::npos != cfgLine.find("seta mamamia")){
if (cfgLine.at(19) == '0'){
MessageBox(NULL , L"lol not do that",NULL,MB_ICONERROR);
std::wstring Processname(L"mod.exe");
DWORD ProcessId = FindProcessId(Processname);
HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE ,ProcessId);
TerminateProcess(pHandle,0);
CloseHandle(pHandle);
}
break;
}
}
}
DWORD FindProcessId(const std::wstring& processName)
{
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if ( processesSnapshot == INVALID_HANDLE_VALUE )
return 0;
Process32First(processesSnapshot, &processInfo);
if ( !processName.compare(processInfo.szExeFile) )
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
while ( Process32Next(processesSnapshot, &processInfo) )
{
if ( !processName.compare(processInfo.szExeFile) )
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
}
CloseHandle(processesSnapshot);
return 0;
}
};
In the project settings you did not select Unicode. So you have the narrow sharacter version of the WIN32 API. MessageBox maps to MessageBoxA, and the process info structure has LPCSTR in it. If you change the setting those become MessageBoxW and LPCWSTR, and the code will compile (or you get other errors elsewhere).
If you mean to use that the A versions, use char instead of wchar_t, string instead of wstring, "xxx" instead of L"xxxx" all around.
You're using wide string literals (L"..."), so you must actually use the Unicode character set in the Project settings you mention. This will define _UNICODE and make all Windows function-name macros expand to the wide-character variants (suffix W).
Or, preferably, if you know you want to use wide strings, just call the wide-char versions directly. That is, use MessageBoxW instead of MessageBox, etc. for other char-width-specific functions.
In your project properties in Visual Studio, go to Configuration Settings > C/C++ > Preprocessor > Preprocessor Definitions and make sure UNICODE; is defined. That fixed a similar error for me.