MASM calling C compiled with Visual Studio Express 2005

Started by asmman, April 02, 2007, 01:08:52 AM

Previous topic - Next topic

asmman

Hi All,

I have spent the better part of this weekend trying to figure this out and I am confused.  I am trying to determine if it is possible to call C code from MASM Asm code and if so what I need to do in order to accomplish this.  From reading the Boards it looks like C code is callable from MASM and vice versa but I am having zero luck getting this to work.  Here are the steps I have followed so far (this is a higlhlevel summary of my work, I will post the code for review also)

1) Created a simple C program in Visual Studio Express 2005 with extern int __stdcall asmPrintFunc(char *msgOut); as the prototype statement.  The program compiles, links and executes with an RC=0.  If I try adding (from some examples on the boards here...) extern "C" int __stdcall asmPrintFunc(char *msgOut); I get error C0259 Syntax error "string" when I compile.

2) Created a MASM program with the prototype asmPrintFunc PROTO C :PTR SBYTE.

3) Run a .bat file to run lib.exe to make a library from the C object files and compile and link the MASM test program.  The compile for the MASM program works fine but if I use the .lib as input to the link I get an LNK2001 Unresolved external symbol _asmPrintFunc.  If use the .obj files from C as input to the link I get an LNK4044 unrecognized option manifestdependency:type=win32.... LNK1104 cannot open MSVCRTD.lib.

I think this is doable but I have a feeling there are one or more pieces of the puzzle that I am missing so if someone can point me in the right direction I will be grateful.
I also have a couple of questsions:

1. Is this even possible to call C code compiled in Visual Studio 2005 Express?  I using it mostly because it's Microsoft and I want to use the PSDK.  If it's not possible to use Visual Studio 2005   what C compiler can I use that will allow me to work with the PSDK?

2. Is the syntax in MASM (or C) to declare functions different when MASM is calling C vs when C is calling MASM?  I found lots of examples on the boards about C calling MASM but could not find much help with MASM calling C (maybe I was not searching on the right topics?)

Thanks!



[attachment deleted by admin]

hutch--

It works the same in both directions as long as you know what calling convention is used in both and prototype accordingly. Where you can get problems is if you don't use the appropriate EXTERN syntax for your particular compiler and end up with mangled names.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

asmman

Thanks for the response hutch but I am still confused:

extern int __stdcall asmPrintFunc(char *msgOut);  /* <== My C Code Function Prototype

;My ASM Code snippet...
includelib C:\MASM32\WinAsm\Projects\AsmCallC\AsmTest.lib
;EXTERN C asmPrintFunc
;EXTERN _asmPrintFunc:NEAR
asmPrintFunc PROTO NEAR C, msgPrint:PTR SBYTE, msgPrint:VARARG

.data
msgPrint db"Test Call from AsmCallC to asmPrintFunc",0,13

.code
AsmCallC:
push OFFSET msgPrint ;Set Addr of msg to print
call asmPrintFunc ;Call asmPrint Func C Func
;invoke asmPrintFunc, OFFSET msgPrint
;ret
end AsmCallC

I am still getting an error LNK2001 unresolved external symbol _asmPrintFunc.  I know because I am using stdcall linkage that the asmPrintFunc name is mangled but what I don't understand is what I need to do to correct it.   I have tried changing the prototoypes in both C and ASM but still can't get the code to be linked.  I can't seem to specify using C linkage by using "extern C" because it is not recognized by the compiler (Microsoft).  I did find a hit in Microsoft for a problem with Microsoft Transaction Server V1.0 not allowing extern "C" to be used but I have no idea if I have Microsoft Transaction Server on my machine or what version it is.  I have tried multiple versions of the EXTERN and PROTO statements in the example and I still am unable to link the code.  Please give me something else to try or I will give up on ASM if it's this much of a pain to call a C function.

Help ! :dazzled:

GregL

asmman,

Do you want to do this with C or C++? It is done differently depending on which you are using and it's easier to do with C. Like Hutch said you have to deal with "mangled" names with C++.

If I remember correctly you use "extern C" with C++ and "extern" with C.

It's more common to call MASM procedures from C (or C++). I attached an example of calling MASM from C, it's for Visual C++ 2005 Express Edition.

I think Vortex may have an example of calling C (or C++) functions from MASM.



[attachment deleted by admin]

hutch--

hmmmm,

> Please give me something else to try or I will give up on ASM if it's this much of a pain to call a C function.

While this comment is about the best way to be told to "go jump in the lake", listening to at least some of the assistance you have been offered may in fact get you somewhere.

1. Make sure you are not getting "mangled names" in your C object module and thus the suggestion of ensuring the in your C code you prototype it with the EXTERN notation defined by your C compiler.
2. make sure that the prototype for you C code matches the form that it is being called with in your asm code.

From the code you posted in, have a look at this,

1. extern int __stdcall asmPrintFunc(char *msgOut);  /* <== My C Code Function Prototype
2. asmPrintFunc PROTO NEAR C, msgPrint:PTR SBYTE, msgPrint:VARARG

The C prototype is __stdcall while the asm prototype is C. Your two code sources are not speaking the same language.

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

GregL

asmman,

I modified your code into a working example. It uses C, not C++.

I updated the attached file, it works with Pelle's C, VC 6.0 and VC 2003, but I could not get it to work with VC 2005 due to the dependency on MSVCR80.DLL. See this for details. It's probably possible to get it working with VC 2005 but is it worth the effort?





[attachment deleted by admin]

asmman

Greg and Hutch,
First thank you both for your help.   I really do appreciate it. Hutch, sometimes my temper gets the better of me and I say things I shouldn't,  my comment about abandoning ASM calling C altogether was definately one of those times.  If someone had told me to go jump in a lake I would have deserved it....  :red

I have spent another two days trying to get this to work and have had zero luck, I still can not get the link to work with the C and ASM code.  I agree with Greg that the problem is somewhere with Visual Studio Express 2005.  I keep getting errors in the link for objects and libraries that in some cases don't appear to be on my machine.  I am contemplating uninstalling it and then installing Pelle's C compiler and then trying this all over again.  At this point my only fear is that uninstalling Visual Studio Express 2005 will cause me more grief but I really do want to be able to call C code from ASM and vice versa so I don't think I have many other choices.

I will need to start researching the possibilities but thank you for your help.

Asmman