Hi,
I`ve written a DLL library in MASM, and I want to use some of its functions in my C++ project. But unfortunately, when I call a function it seems to ignore the second parameter. Also there are some noises in wave file (the function adds an echo effect) which I don`t know where do they come from.. :(
Open_wave_file proc C Plik:DWORD
invoke CopyFile,Plik, addr NewFileName, FALSE
invoke CreateFile, addr NewFileName,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ OR FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
mov hDebugFile, eax
Ret
Open_wave_file EndP
;-------------------------------------------------------------------------------------
Modify_wave proc C StTlumienia: DWORD, Plik: DWORD
mov eax, StTlumienia
mov Divider, eax
mov RepeatNo, 3
mov EchoBufferSize, 2000h
push Plik
call Open_wave_file
[...some other stuff....]
In my C++ file I declare a pointer to function:
typedef void (WINAPI*DllFunc)(int, void*);
HINSTANCE EchoDLL;
DllFunc _Modify_wave;
I load library using LoadLibrary and then call the function:
EchoDLL = LoadLibrary("DLL.dll");
_Modify_wave = (DllFunc)GetProcAddress(EchoDLL,"Modify_wave");
_Modify_wave(X, &Y); //X is int type variable, and Y is CString
maybe a little OT but could you explain why are you using typedef in your function pointer declaration,
I thought that this is right syntax:
Return_Type (*Pointer_Name)(Parameters)
Actually I don`t know:) I get used to that kind of declaration. But as far as I know it doesn`t change anything.
If you want to declare two or more pointers to function that takes the same arguments you can use typedef because it makes things easier:
For example instead of:
void (*my_pointer1)(int, int);
void (*my_pointer2)(int, int);
void (*my_pointer3)(int, int);
you could write:
typedef void (*my_pointer)(int, int);
my_pointer func1;
my_pointer func2;
my_pointer func3
Perhaps CopyFile requires a char *, not a CString?
If so, void * will not provide the correct data conversion.
Copyfile (http://msdn2.microsoft.com/en-us/library/aa363851.aspx) expects a LPCTSTR, which is a LPCWSTR if UNICODE is defined, otherwise it's an LPCSTR, which is a pointer to a null-terminated ANSI string (see Windows Data Types (http://msdn2.microsoft.com/en-us/library/aa383751.aspx)). In C++ code you can apparently substitute CString objects for const char* and LPCTSTR function arguments and the compiler will handle the conversion, so I think tenkey is on the right track here. I would try changing the declaration to const char* or LPCTSTR, or passing (LPCTSTR) Y instead of &Y. If these methods will not work, then there are methods of forcing the conversion:
http://msdn2.microsoft.com/en-us/library/awkwbzyc(VS.71).aspx
You're passing the address of the CString. If you leave out the & operator and change the prototype from void* to const char* or LPCSTR it should work.
I can't remember if CString will convert to a wide string but by default the MASM libraries use the ANSI string versions of functions so you are probably best off passing it as an LPCSTR rather than LPCTSTR or LPCWSTR.
Cheers,
Zooba :U
Voila! I replaced void* with LPCSTR and it works :bg
But it crashes after exiting DLL function :( What could be the reason?
Hello,
Your functions are declared _cdecl in your asm source. Your declaration of them in your c++ module appears to be _stdcall = bad calling convention = screwed stack when it returns.
Regards,
Tim