Can I create a C++ class library(DLL) and use it in C#?

G

Guest

Hi,

I have a few C BER coding that I would like to compile into a C++ Class
library project and then use it in my C# windows application. Can I do that
from C# using pinvoke? If not then does anyone how can I call these
functions from C#?
 
B

Bruno van Dooren [MVP VC++]

I have a few C BER coding that I would like to compile into a C++ Class
library project and then use it in my C# windows application. Can I do
that
from C# using pinvoke? If not then does anyone how can I call these
functions from C#?

Hi,
You don't have to create a class library.
Simply export those functions from a dll as C functions, using a DEF file or
using __declspec(dllexport)
You can then use P/Invoke those functions in your C# application.
That would be the easiest solution.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
G

Guest

Thank you.


Bruno van Dooren said:
Hi,
You don't have to create a class library.
Simply export those functions from a dll as C functions, using a DEF file or
using __declspec(dllexport)
You can then use P/Invoke those functions in your C# application.
That would be the easiest solution.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
S

Shekhar

One thing worth remembering is: you can use only declspec calling
convention....

-Shekhar
 
B

Bruno van Dooren [MVP VC++]

One thing worth remembering is: you can use only declspec calling
convention....

Actually, declspec is not a calling convention. It's a declaration
specifier.
And you can use __stdcall, __fastcall or __cdecl calling convention.
Whatever is fit for your purpose.
Afaik there is no restriction on calling convention when using DllImport in
C#.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
G

Guest

Funny you should mention the calling conventions. I'm getting LNK2019 error
when I tried to create the my C++ DLL and export it. Here is my export
statement:
#include "stdafx.h"
extern "C" DE_ERRORS __declspec(dllexport)DecodeAsnUser(Blob* pBlob,
CUserContextData *userData)
{
my code;
}
Here are my errors:
-------------------------------------------------------
Error 1 error LNK2019: unresolved external symbol "void __stdcall
_com_issue_error(long)" (?_com_issue_error@@YGXJ@Z) referenced in function
"public: class _bstr_t & __thiscall _bstr_t::blush:perator=(char const *)"
(??4_bstr_t@@QAEAAV0@PBD@Z) UnityDecodeAsnUser.obj

Error 2 error LNK2019: unresolved external symbol "wchar_t * __stdcall
_com_util::ConvertStringToBSTR(char const *)"
(?ConvertStringToBSTR@_com_util@@YGPA_WPBD@Z) referenced in function "public:
__thiscall _bstr_t::Data_t::Data_t(char const *)"
(??0Data_t@_bstr_t@@QAE@PBD@Z) UnityDecodeAsnUser.obj

Error 3 error LNK2019: unresolved external symbol "char * __stdcall
_com_util::ConvertBSTRToString(wchar_t *)"
(?ConvertBSTRToString@_com_util@@YGPADPA_W@Z) referenced in function "public:
char const * __thiscall _bstr_t::Data_t::GetString(void)const "
(?GetString@Data_t@_bstr_t@@QBEPBDXZ) UnityDecodeAsnUser.obj

Error 4 fatal error LNK1120: 3 unresolved
externals C:\Projects\UnityDecodeAsnUser\Debug\UnityDecodeAsnUser.dll

How can I correct this Lnk error?
 
G

Guest

Ok, I seem to get rid of the lnk2019 error by doing the following:
link "comsuppwd.lib" in debug AdditionalDependencies.
Link "comsuppw.lib" in release AdditionalDependencies
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top