0

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,

Khaleel Hmoz
  • 987
  • 2
  • 13
  • 24
  • 1
    Shouldn't that be `extern "C" int nGetSomeThing(void);` in C++ for a function (to be used as a static method in C#)? – Basile Starynkevitch Jan 09 '14 at 08:05
  • You can use `unsafe` keyword? Look at this: [link](http://social.msdn.microsoft.com/Forums/en-US/bf8fb136-c2a8-417d-8e06-3d20b035ec1b/cc-pointer-function-dll-calling-from-c-application?forum=csharpgeneral) – mike00 Jan 09 '14 at 08:08
  • I can't change the C++ code, and extern "C" is not exists there :(. – Khaleel Hmoz Jan 09 '14 at 08:48
  • 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* – Khaleel Hmoz Jan 12 '14 at 12:09

3 Answers3

2

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.

Community
  • 1
  • 1
lmt1608
  • 84
  • 9
  • 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* – Khaleel Hmoz Jan 12 '14 at 12:09
  • Your Function: GetSomthing(const char* szConfigPath, char *A, int B). I think: char* szConfigPath in C++<==> string szConfigPath in C#, but i don't know what is the "A" variable?, Can you give me some value of the "A" variable. – lmt1608 Jan 12 '14 at 12:25
  • my problem was somthing else, still didn't understod it but it worked when i used #ifdef __cplusplus – Khaleel Hmoz Jan 12 '14 at 14:19
  • Hi, I'm not a progamer with c++, but you can find somthing about the #ifdef __cplusplus in here: http://www.cplusplus.com/doc/tutorial/preprocessor/ , and Does your function worked?. Sorry, i'm trying to learning English.If you don't understand, please tell me.:D – lmt1608 Jan 12 '14 at 14:57
  • No! your so good... Thanks BTW, :) and yes I fixed my problem finally :) – Khaleel Hmoz Jan 12 '14 at 18:44
1

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

jester
  • 3,491
  • 19
  • 30
  • 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* – Khaleel Hmoz Jan 12 '14 at 12:08
0

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

}

Community
  • 1
  • 1
Khaleel Hmoz
  • 987
  • 2
  • 13
  • 24