Question

I'm not that good in C++ but,.. For example I have this

#define GETSomthing_API  __declspec(dllexport)
extern GETSomthing_API int nGetSomthing;

And the method I want to import like this

GETSomthing_API  int GetSomthing(const char* szConfigPath, char *A, int B)

How Can I call this from C# ?

Beside, I think my problem is with the parameter type (const char* ) in C++ side, what is the equal type in C# for it! const char*

Thanks,

Was it helpful?

Solution

How to call C++ code from C#

or

Calling C++ function from C#

Using: [DllImport("xxx.dll")] xxx.dll is compile by C++

Hope this help.

OTHER TIPS

There are a couple of ways for calling into native code from C#. The easiest is probably to use P/Invoke. Say your function is like :

extern "C" int nGetSomeThing(void);

and it is compiled into a YourDllName.dll file, you can use P/Invoke to directly call into the unmanaged code in the following way from C# code :

public static class interop
{
    [DllImport("YourDllName.dll")]
    static extern public int nGetSomeThing(void);
}
...
interop.nGetSomething() // call to the native function

Refer : http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx.

If you have a lot of functions with complex signatures, you should probably go for an interop layer in C++/CLI. Refer : http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes

I just added __cplusplus check and it worked!

#ifdef __cplusplus

extern "C" {

endif

GETSomthing_API char* GetAgentId(const char* szConfigPath, char *szA, int ASize);

ifdef __cplusplus

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top