473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C++ DLLs in C#

Rob
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.
May 10 '06 #1
7 1376
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]
- mv*@spam.guard.caspershouse.com

"Rob" <Ro*@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...
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.

May 10 '06 #2
Rob
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Rob" <Ro*@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...
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.


May 10 '06 #3
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]
- mv*@spam.guard.caspershouse.com

"Rob" <Ro*@discussions.microsoft.com> wrote in message
news:2E**********************************@microsof t.com...
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Rob" <Ro*@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...
> 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.


May 10 '06 #4

"Rob" <Ro*@discussions.microsoft.com> wrote in message
news:2E**********************************@microsof t.com...
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
May 10 '06 #5
>[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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 10 '06 #6
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

Rob wrote:
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.

May 11 '06 #7
_DD
On Wed, 10 May 2006 08:45:02 -0700, Rob
<Ro*@discussions.microsoft.com> wrote:
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.
May 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Johann Blake | last post by:
I can hardly believe I'm the first one to report this, but having gone through the newsgroup, it appears that way. I would like to open a solution in the VS.NET IDE that consists of multiple...
2
by: Shiraz | last post by:
Hi I just made an installer for an application that uses two external COM dlls. On the surface, everything seems to be running smoothly and the the application runs without any errors. However,...
11
by: Devender Khari | last post by:
Hi Friends, I'm facing a situation as follows, need help on identifying possible issues. There is an MFC application developed in VC6.0, say ABCVC6.exe and another developed in VC.NET, say...
0
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs...
7
by: Oenone | last post by:
I'm sure there's an obvious way to do this, but I'm missing it so far. I have an ASP.NET application that relies on several DLLs to work. Currently in order to get my site working I have to put...
6
by: Brian Bischof | last post by:
I'm having troubles getting the debugging process to work consistenly for external classes. I got it to work once and then I turned it off. But now I can't get re-enabled. Here is what I'm doing....
0
by: Dave | last post by:
Hello The application I'm building an installer for uses dlls which were developed originally in C. Since the application itself is developed in C#, these dlls were wrapped using SWIG....
7
by: Jeff Lynn | last post by:
Help! I recently upgraded my VS V6 to VS 2005 and was unable to build projects that were perfectly ok under VS V6. Where VS 2005 fails was in the linker resolving external DLLs, which are Open...
3
by: gopal | last post by:
I am developing an application in CSharp - windows forms based, which copies the DLLs both unmanaged and managed DLLs from a shared folder and will overwrite the existing versions of managed &...
10
by: =?Utf-8?B?UmljaGFyZA==?= | last post by:
Hi, I usually deploy my ASP .NET application to the server by publishing, using Visual Studio 2005 publish feature. This creates the Bin folder on the server, with the compiled DLLs. I've...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.