forgive me if this sounds stupid: How can i call a C function from masm32? Specificly i want to call the function "randomize" and some others, i am trying to generate some random numbers. But what i am doing is not important. the Important thing is IF i can call C functions and how. thanks a lot to all. Ray.
Posted on 2001-02-01 01:14:00 by Ray
It depends, if you mean build in compiler functions, such as malloc etc, they are built into the C compiler. So are not availible to use unless you are using that compiler. If the function is in a dll you can call the function in two ways. One way is with GetProcAddress, and the otherway is when a library is linked to your app, and is staticly loaded and you call the functions via your apps import table using invoke. To use the dll via the second method, you have to have the import librarys for the dll. (DllName.lib usaly). And use it the same way you would use an import library for say, kernel32.dll. If you do not have a library such as. Use the something like the following: .data DllName BYTE "CDll.dll",0 FuncName BYTE "CFunc",0 .data? hDll HANDLE ? .code invoke LoadLibrary ,ADDR DllName mov hDll,eax invoke GetProcAddress ,hDll,ADDR FuncName push arg1 push arg2 . . . push argn call dword ptr eax You do however, have to double check the number of arguments passed to the dll, as there are no compile time checks as when using invoke. If you know the name of the function and this method is not working. Try using 'dumpbin /EXPORTS CDll.dll' This will show you the proper function names. As the could of become decorated by the linker.
Posted on 2001-02-01 03:36:00 by X
Thanks 'X' , most probably the function that i want to call resides in the C compiler as you mentioned, therefore i can't use it. regards. Ray.
Posted on 2001-02-01 04:27:00 by Ray
You can call C funcions from MASM32 package. The restriction is you can call only C runtime functions and not all functions. Just see a "quick view" on the "CRTDLL.DLL", that is a C runtime library function DLL. It does not have randamize though, but has rand(). Following code snippet will give an idea how to use these functions. Note that all the argument for C runtime functions are in DWORD. I assume CRTDLL.DLL is present in every machine by default. .data szMsg DB "Hello from C Runtime Libaray function", 0 .code start: Push szMsg ; Argument to puts function Call puts ; Call puts Add esp, 4 ; Adjust stack manually due to C calling convetion Push 0 ; Return code for OS Call ExitProcess ; Exit End start X: You can use malloc() also. This message was edited by sjhenry, on 2/1/2001 12:28:10 PM This message was edited by sjhenry, on 2/1/2001 12:29:50 PM This message was edited by sjhenry, on 2/1/2001 12:30:16 PM
Posted on 2001-02-01 12:26:00 by sjhenry
Thanks Henry, thats the kind of answer I was hoping for :)
Posted on 2001-02-02 01:30:00 by Ray
Hmm... It seems to me that you can call any C function without restriction from MASM32, but you have to use call, not invoke. I have done this quite often. There is a problem with MASM32 decorating C-function names, which I've gotten around by using a utility to undecorate the MASM32-generated obj file, given a user-provided list of names (of the C functions). The undecorated obj file links OK, which means that all the function calls are being correctly resolved in the necessary dll's. HOWEVER, although the resulting exe looks ok when disassembled it tends to bomb in the start-up code. I'm trying to understand why -- looks like it's a long story.
Posted on 2001-02-02 17:24:00 by Xmas
Some things to watch out for. Unlike the Win32 API, the C runtime routines use the __cdecl calling convention. You must clean up the stack after a call. If the prototypes indicate CDECL, then INVOKE should automatically do this for you. Be aware that some functions (mainly file I/O) assume some hidden initialization code has been executed before they are called. And don't forget that the functions can trash ECX and EDX in addition to EAX. This message was edited by tank, on 2/2/2001 8:11:38 PM
Posted on 2001-02-02 19:30:00 by tank