Importing C++ dll into C#

P

Parrot

I have tried everything to import a C++ dll into a C# program without
success. I can import the dll into another C++ program but not a C# program.
I always get the message that it can't find the function in the specified
dll. So it is finding the dll program but it can't find the entry
statements. I know that in C++ environment a lib file is used for addressing
the entry statements at link time. Obviously, it does not work in a C#
environment since there are no linker variables. Does anyhow have an example
of importing a C++ dll into a C# program using the DLLexport function?
Dave
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

Are you trying to import a regular C++ dll with classes, or a DLL that
exports functions? Or is it a COM dll? If it is a native dll with exported
functions, then you need to create the declarations in C# to make the call
through the P/Invoke layer. If it is a COM dll, then you should set a
reference in your project, and let the IDE create interop dlls for you (or
do it yourself with the TLBIMP utility). If it is a C++ dll with just
regular classes, you are going to have to expose the functionality through
COM (and import it into your project) or create a managed wrapper in C++ and
access that.
 
M

Michael C

Parrot said:
I have tried everything to import a C++ dll into a C# program without
success. I can import the dll into another C++ program but not a C#
program.
I always get the message that it can't find the function in the specified
dll. So it is finding the dll program but it can't find the entry
statements. I know that in C++ environment a lib file is used for
addressing
the entry statements at link time. Obviously, it does not work in a C#
environment since there are no linker variables. Does anyhow have an
example
of importing a C++ dll into a C# program using the DLLexport function?
Dave

Here's an example. You just put this code somewhere in you C# project. This
is all you need, you don't add an import like you do for C++.
[DllImport("gdi32")]
private static extern int AbortDoc(int hdc);

You need to make sure your C++ dll doesn't mangle the function names it
exports and generally you should make then stdcall. To stop the mangling you
need to add a text file called Exports.DEF and add something like this

Exports
FirstFunctionNameNotToMangle
SecondFunctionNameNotToMangle
etc
 
P

Parrot

Thanks for your reply. I can import a C++ dll into my C# program but I have
to use an ordinal number in order to have C# program recognize my entry point
such as "EntryPoint = "#2"; it will not recognize "EntryPoint = "CALL1" for
example.

My biggest problem is that I am trying to import legacy COBOL programs from
Microfocus and my C# program will not even recognize the dll program itself
although this may be masking the fact it can't recognize the entry points. I
use an "EntryPoint # for my COBOL dll but that didn't help. So I imported a
COBOL program into my C++ dll and then had my C# program call C++ and then it
wouldn't recognize the dll just like for the COBOL dlls. So I am totally
confused as to why using COBOL dll's screws everything up. I copied the dll
and lib files into my directory and even tried to just do a call with an
empty function and still not working. I want to be able to access my COBOL
programs through C# but it looks like I am stuck with C++ unless I can figure
out what's going on. Here is my code in C# so far:

[DllImport("c:/development/datatron/datatron/bin/mfcdll.dll", EntryPoint =
"#2", SetLastError = false,
CharSet = CharSet.Unicode, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
private static extern int CALL1([MarshalAs(UnmanagedType.LPStr)]
String text);
............
string filename = "FILE02";
int ret = CALL1(filename);

My COBOL program has the following code:

LINKAGE SECTION.
01 LINK-FILE PIC X(50).

ENTRY 'CYREAD' using BY REFERENCE LINK-FILE.
....

Also, I am using a 32bit Microfocus compiler from 1998 to create the dll's.
I can't get much help from Microfocus since I am no longer on maintenance.


Nicholas Paldino said:
Dave,

Are you trying to import a regular C++ dll with classes, or a DLL that
exports functions? Or is it a COM dll? If it is a native dll with exported
functions, then you need to create the declarations in C# to make the call
through the P/Invoke layer. If it is a COM dll, then you should set a
reference in your project, and let the IDE create interop dlls for you (or
do it yourself with the TLBIMP utility). If it is a C++ dll with just
regular classes, you are going to have to expose the functionality through
COM (and import it into your project) or create a managed wrapper in C++ and
access that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Parrot said:
I have tried everything to import a C++ dll into a C# program without
success. I can import the dll into another C++ program but not a C#
program.
I always get the message that it can't find the function in the specified
dll. So it is finding the dll program but it can't find the entry
statements. I know that in C++ environment a lib file is used for
addressing
the entry statements at link time. Obviously, it does not work in a C#
environment since there are no linker variables. Does anyhow have an
example
of importing a C++ dll into a C# program using the DLLexport function?
Dave
 

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