views:

470

answers:

3

I'm new to Visual C++, .NET, and the Windows world of programming (coming from Objective-C/Cocoa), and I'm trying to use the CFLite (Open CoreFoundation Lite) library, which I compiled to a .lib file. It's written in pure, unmanaged C, and I'd like to use it in my managed CLR .NET app. When I try to link it and use the function CFSTR, which is a shortcut for __CFStringMakeConstantString, it fails horribly. Any advice? Attached is the error.

Proj.obj : error LNK2031: unable to generate p/invoke for "extern "C" struct __CFString const * __clrcall __CFStringMakeConstantString(char const *)" (?__CFStringMakeConstantString@@$$J0YMPBU__CFString@@PBD@Z); calling convention missing in metadata
Proj.obj : warning LNK4248: unresolved typeref token (01000016) for '__CFString'; image may not run
Proj.obj : error LNK2028: unresolved token (0A00000B) "extern "C" struct __CFString const * __clrcall __CFStringMakeConstantString(char const *)" (?__CFStringMakeConstantString@@$$J0YMPBU__CFString@@PBD@Z) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)
Proj.obj : error LNK2019: unresolved external symbol "extern "C" struct __CFString const * __clrcall __CFStringMakeConstantString(char const *)" (?__CFStringMakeConstantString@@$$J0YMPBU__CFString@@PBD@Z) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)

UPDATE: I ended up just going with a native application with Win32 calls, since my user interface is going to be minimal anyway (it's a background app). I played around with p/invoke a little, and it just seemed to complicated for a newbie like me :p Thanks!

A: 

The ways I know to work with managed code from unmanaged is to build a DLL or to work with COM.

Drahakar
+1  A: 

It looks like you have a mismatch in the function signature you are using in your managed C++ app compared to the one in your C library.

The technology .Net uses to communicate between the managed World and the unmanaged World is PInvoke. The best resource for examples of how to use PInvoke is pinvoke.net. Your C function signatures look very simple so it should be straight forward to create the matching one in C++ with the parameters correctly marshalled.

sipwiz