News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

MASM & C++ -problems with calling functions

Started by piotrus, May 21, 2007, 05:48:10 PM

Previous topic - Next topic

piotrus

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

dancho

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)


piotrus

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

tenkey

Perhaps CopyFile requires a char *, not a CString?

If so, void * will not provide the correct data conversion.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

MichaelW

Copyfile 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). 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


eschew obfuscation

zooba

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

piotrus

Voila! I replaced void* with LPCSTR and it works  :bg
But it crashes after exiting DLL function  :( What could be the reason?

Timbo

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