-
August 16th, 2010, 01:21 AM
#1
LoadLibrary+ GetporcAddress to call function form custom dll
Hi,
I want to call function "asdf" from dl "x.dll":
Code:
typedef int (*MYPROC)(LPVOID adr,BYTE* arg0,int arg2);
HANDLE testh;
LPVOID adr;
MYPROC dllfunc;
BYTE arg0[] = {0x01, 0x01, 0x01, 0x01, 0x00};
int arg2 = 5;
int retval;
testh = CreateFileA("C:\\asdf.exe",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
testh = CreateFileMappingA(testh,0,PAGE_READWRITE,0,filesize,NULL);
addressx = ::MapViewOfFile(testh, FILE_MAP_WRITE,0,0,0);
BYTE *arg0de = &arg0[0];
testh = LoadLibraryA("x.dll");
dllfunc = (MYPROC)GetProcAddress((HMODULE)testh,"asdf");
retval = dllfunc(adr,arg0de,arg2);
This code works perfectly fine when compiled in a Win32 console project. WHen compiled in a managed C++ form project (button click event) it throws a access violation exception when calling the function....?
-
August 16th, 2010, 08:05 AM
#2
Re: LoadLibrary+ GetporcAddress to call function form custom dll
Code:
testh = LoadLibraryA("x.dll");
assert(testh);
dllfunc = (MYPROC)GetProcAddress((HMODULE)testh,"asdf");
assert(dllfunc);
retval = dllfunc(adr,arg0de,arg2);
Do either of the asserts fail?
-
August 16th, 2010, 08:42 AM
#3
Re: LoadLibrary+ GetporcAddress to call function form custom dll
No.
-
August 16th, 2010, 08:42 AM
#4
Re: LoadLibrary+ GetporcAddress to call function form custom dll
is it a 64bit OS your running your code , i.e win 7 64 bit , if so go to project options make sure u use .NET 32bit , and then compile
-
August 16th, 2010, 09:00 AM
#5
Re: LoadLibrary+ GetporcAddress to call function form custom dll
Sorry, forgot to mention that. I am compiling & testing on a 32 bit XP SP3.
-
August 16th, 2010, 09:25 AM
#6
Re: LoadLibrary+ GetporcAddress to call function form custom dll
Where exactly does the code crash? When calling the function, or within the function? Step through the code and watch your values.
Are your function declaration and the actual function using the same calling standard? I think VC++ defaults to __stdcall, but most library interfaces use __cdecl
-
August 16th, 2010, 09:51 AM
#7
Re: LoadLibrary+ GetporcAddress to call function form custom dll
It crashes within the external dll. All the values in my code are correct.
Not sure about these calling standards, but since the same code works fine in an win32 project, it should be ok... (anyways, where can i change that?).
The dll is written in MASM.
thx for you help so far!
-
August 16th, 2010, 09:57 AM
#8
Re: LoadLibrary+ GetporcAddress to call function form custom dll
My guess is that it's a calling convention incompatibility. Being an assembly programmer myself, I'm betting that the library uses cdecl convention.
Honestly, I'm not sure how to change the typedef, and I think Microsoft it does it differently than GNU, but you can try this:
Code:
typedef int (*MYPROC)(LPVOID adr,BYTE* arg0,int arg2) __attribute__((cdecl));
If that doesn't work then one of the Windows programmers around here might have to help.
-
August 16th, 2010, 11:01 PM
#9
Re: LoadLibrary+ GetporcAddress to call function form custom dll
I just emailed the author of the dll, it uses stdcall.
-
August 16th, 2010, 11:19 PM
#10
Re: LoadLibrary+ GetporcAddress to call function form custom dll
Hey, i GOT it!
The masm dll is compiled for __stdcall, so you have to define your typedefas __stdcall:
typedef int (__stdcall *MYPROC)(LPVOID adr,BYTE* arg0,int arg2);
Now it works just fine!
Thanks for your help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|