Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pucca - 28 Aug 2006 23:27 GMT
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#?

Signature

Thanks.

Bruno van Dooren [MVP VC++] - 29 Aug 2006 05:43 GMT
> 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.

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Pucca - 29 Aug 2006 18:40 GMT
Thank you.

> > 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
[quoted text clipped - 8 lines]
> You can then use P/Invoke those functions in your C# application.
> That would be the easiest solution.
Shekhar - 29 Aug 2006 20:15 GMT
One thing worth remembering is: you can use only declspec calling
convention....

-Shekhar
> Thank you.
>
[quoted text clipped - 11 lines]
>> You can then use P/Invoke those functions in your C# application.
>> That would be the easiest solution.
Bruno van Dooren [MVP VC++] - 29 Aug 2006 20:41 GMT
> 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#.

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Pucca - 29 Aug 2006 23:51 GMT
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::operator=(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?  

Signature

Thanks.

> > One thing worth remembering is: you can use only declspec calling
> > convention....
[quoted text clipped - 5 lines]
> Afaik there is no restriction on calling convention when using DllImport in
> C#.
Pucca - 30 Aug 2006 00:36 GMT
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
Signature

Thanks.

> 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
[quoted text clipped - 38 lines]
> > Afaik there is no restriction on calling convention when using DllImport in
> > C#.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.