Calling a C++ dll from C#

A

andanb

Hi all,

I have been struggling with the following problem and I hope that you
will be able to help me out:

-How can I create a dll file in Visual Studio?
-How can I call the files from C#?

I would appreciate your help!

Thanks,
An
 
A

Alex Meleta

Hi,

A .dll file doesn't mean C++ only file. You can also create .net dll. If
so, then simply create Control Library project, make public interfaces and
methods and add the library (.dll) to any other projects using References
folder of project (add reference).

There are very good examples:
http://www.c-sharpcorner.com/UploadFile/mahesh/dll12222005064058AM/dll.aspx
http://msdn2.microsoft.com/en-us/library/7314433t(VS.80).aspx

But, if you however need the C++ library then the conversation below is just
for you:
[Create native DLL in c++ and call it from c#]
http://www.thescripts.com/forum/thread589035.html

Regards, Alex
[TechBlog] http://devkids.blogspot.com
 
A

andanb

Thanks for the quick reaplies ! I ran into other issues:

I used the same approach as the one described here:
http://blogs.msdn.com/jonathanswift/archive/2006/10/02/780637.aspx

The only catch is that the project which I am trying to compile into a
dll is half written in C and the other half in C++. What were the
developers thinking !?! I am using Visual Studio 2005 and to create
the dll, I ran New Project -> Win32 Project -> Application Settings ->
DLL (plus the additional option of exporting symbols). Then I declare
all the functions in the MyDll.h and also include the header file of
the original project in stdafx.h. After including the latter header in
the *.c and *.cpp files the following error was thrown:

"fatal error C1853: 'Debug\MyDll.pch' precompiled header file is from
a previous version of the compiler, or the precompiled header is C++
and you are using it from C (or vice versa)"

If I don't include stdafx.h in the *c and *cpp files the following
error is thrown:

"fatal error C1010: unexpected end of file while looking for
precompiled header. Did tou forget to add '#include "stdafx.h" to your
source'"?

Can anyone help me out? I would really appreciate your input?

Thanks,
An
 
B

Ben Voigt [C++ MVP]

Thanks for the quick reaplies ! I ran into other issues:

I used the same approach as the one described here:
http://blogs.msdn.com/jonathanswift/archive/2006/10/02/780637.aspx

The only catch is that the project which I am trying to compile into a
dll is half written in C and the other half in C++. What were the
developers thinking !?! I am using Visual Studio 2005 and to create
the dll, I ran New Project -> Win32 Project -> Application Settings ->
DLL (plus the additional option of exporting symbols). Then I declare
all the functions in the MyDll.h and also include the header file of
the original project in stdafx.h. After including the latter header in
the *.c and *.cpp files the following error was thrown:

"fatal error C1853: 'Debug\MyDll.pch' precompiled header file is from
a previous version of the compiler, or the precompiled header is C++
and you are using it from C (or vice versa)"

If I don't include stdafx.h in the *c and *cpp files the following
error is thrown:

"fatal error C1010: unexpected end of file while looking for
precompiled header. Did tou forget to add '#include "stdafx.h" to your
source'"?

Take "stdafx.h" out of the .c files, and then in compile properties, also
turn off precompiled headers (there's a section under C++ Compiler called
Precompiled Headers).
 

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