C++ DLLs in C#

G

Guest

Hi, what I'm working on right now is putting my old C++ code into a DLL, and
accessing it from a C# application... but I'm running into some problems.
I need to pass and receive strings from the DLL, but the data isn't comign
through right. Here's a little test I made:

In the DLL:

bool __declspec(dllexport) CompareClass::LoadGameInfo(LPCWSTR Message)
{
AfxMessageBox(Message);
return true;
}

In C#:

[DllImport("DLL Test client.dll")]
public static extern bool LoadGameInfo(string Message);
LoadGameInfo("test");


The result:
A message box pops up but does not include any message.

I would very much appreciate a tutorial on such matters or simply an
explanation. Thank you.
 
N

Nicholas Paldino [.NET/C# MVP]

Rob,

This looks like an instance member of a class. You should be exporting
this as a plain function, not a member of a class. You shouldn't access
members of C++ classes this way. Rather, you are better off creating a
managed wrapper for the class and creating an instance of it in your code.
Either that, or exposing it through COM, and then exposing it to your code
that way.

Hope this helps.
 
G

Guest

I need to keep it in the class because there are going to be several
functions using the same variables. Unless... can I make an instance of the
class global, then take the functions out of the class and have them access
the global instance for the variables?

Do you know of any articles or tutorials on this subject? I know all you are
very busy and I don't want to be bugging you several times a day with
questions.

Nicholas Paldino said:
Rob,

This looks like an instance member of a class. You should be exporting
this as a plain function, not a member of a class. You shouldn't access
members of C++ classes this way. Rather, you are better off creating a
managed wrapper for the class and creating an instance of it in your code.
Either that, or exposing it through COM, and then exposing it to your code
that way.

Hope this helps.


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

Rob said:
Hi, what I'm working on right now is putting my old C++ code into a DLL,
and
accessing it from a C# application... but I'm running into some problems.
I need to pass and receive strings from the DLL, but the data isn't comign
through right. Here's a little test I made:

In the DLL:

bool __declspec(dllexport) CompareClass::LoadGameInfo(LPCWSTR Message)
{
AfxMessageBox(Message);
return true;
}

In C#:

[DllImport("DLL Test client.dll")]
public static extern bool LoadGameInfo(string Message);
LoadGameInfo("test");


The result:
A message box pops up but does not include any message.

I would very much appreciate a tutorial on such matters or simply an
explanation. Thank you.
 
N

Nicholas Paldino [.NET/C# MVP]

Rob,

You will still have to create wrappers for this to export the functions
and then pass a handle around which represents the instance of the class.

I recommend that you expose your class through COM or managed
extensions, and use it as you would normally.


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

Rob said:
I need to keep it in the class because there are going to be several
functions using the same variables. Unless... can I make an instance of
the
class global, then take the functions out of the class and have them
access
the global instance for the variables?

Do you know of any articles or tutorials on this subject? I know all you
are
very busy and I don't want to be bugging you several times a day with
questions.

Nicholas Paldino said:
Rob,

This looks like an instance member of a class. You should be
exporting
this as a plain function, not a member of a class. You shouldn't access
members of C++ classes this way. Rather, you are better off creating a
managed wrapper for the class and creating an instance of it in your
code.
Either that, or exposing it through COM, and then exposing it to your
code
that way.

Hope this helps.


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

Rob said:
Hi, what I'm working on right now is putting my old C++ code into a
DLL,
and
accessing it from a C# application... but I'm running into some
problems.
I need to pass and receive strings from the DLL, but the data isn't
comign
through right. Here's a little test I made:

In the DLL:

bool __declspec(dllexport) CompareClass::LoadGameInfo(LPCWSTR Message)
{
AfxMessageBox(Message);
return true;
}

In C#:

[DllImport("DLL Test client.dll")]
public static extern bool LoadGameInfo(string Message);
LoadGameInfo("test");


The result:
A message box pops up but does not include any message.

I would very much appreciate a tutorial on such matters or simply an
explanation. Thank you.
 
A

andrew queisser

Rob said:
I need to keep it in the class because there are going to be several
functions using the same variables. Unless... can I make an instance of
the
class global, then take the functions out of the class and have them
access
the global instance for the variables?

Do you know of any articles or tutorials on this subject? I know all you
are
very busy and I don't want to be bugging you several times a day with
questions.

To pass strings I use "StringBuilder" on the C# side and "const char *" on
the C++ side.

To handle passing of object references in one case I put the interface into
'extern "C" ' functions with _stdcall return codes. The C functions take
pointers to the object as a void * for the first argument, like an explicit
this pointer. The C# code then uses the void * as a handle and the 'extern
"C" ' functions cast the handle to the class type and forward to the C++
code.

On the C# side I have a wrapper class that stores the handle and has the
appropriate constructor/accessor functions.

One drawback of this method is that you need some kind of "Open" or "Create"
function for the C# code to initially get a handle for a particular object.

Andrew
 
M

Mattias Sjögren

[DllImport("DLL Test client.dll")]
public static extern bool LoadGameInfo(string Message);
LoadGameInfo("test");

In addition to what the others have said, you should add
CharSet=CharSet.Unicode to the DllImport attribute since the target
method takes a LPWSTR.


Mattias
 
L

Leon Lambert

I highly suggest you look at the new C++ in .Net 2.0. You would not
believe how easy it is to make a managed C++ dll that interacts with
unmanged C++. You include the header files from you old C++ code and it
magically does all the marshalling for you. I stumbled upon this when
looking up #pragma unmanaged in the help and followed and read all the
associated links. The compiler now will create the marshaling for you
and in most cases it will be faster than pInvoke and a whole lot easier
to implement.

Hope this gives you something to look at.
Leon Lambert
 
D

_DD

Do you know of any articles or tutorials on this subject? I know all you are
very busy and I don't want to be bugging you several times a day with
questions.

Rob, Late reply, but there is a new book from APress called ".NET 2.0
Interoperability Recipes", author Greg Bukovics. Excellent reference
for what you want to do, especially given that there's a learning
curve. The recipes will give you some quick, direct results while you
ramp up on things.
 

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