473,386 Members | 2,042 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,386 software developers and data experts.

Import unmanaged C++ class from dll issue

Hi,
I'm trying to import code from a c++ unmanaged dll in a c# project.

In the dll there's a class named Connection, but the dll exposes only
2 methods (outside the class):
- HBITMAP DownloadIntra(char *host, int port, char *user, char *pwd,
int channel);
- Connection* CreateConnection(char *host, int port, char *user, char
*pwd);

My friend was able to import them in a C++ .Net project with:

- extern "C" __declspec(dllimport) HBITMAP __cdecl DownloadIntra(char
*host, int port, char *user, char *pwd, int channel);

- extern "C" __declspec(dllimport) Connection* __cdecl
CreateConnection(char *host, int port, char *user, char *pwd);

He created the Connection class wrapper with virtual methods:
class Connection
{
public:
virtual int __stdcall disconnect() = 0;
virtual int __stdcall authenticate() = 0;
virtual int __stdcall getPort() = 0;
virtual int __stdcall connected() = 0;
virtual int __stdcall getChannelsCount() = 0;
virtual char* __stdcall getThreadID() = 0;
virtual void __stdcall FreeConnection() = 0;
virtual void __stdcall usePing(bool ping) = 0;
virtual bool __stdcall isPingActive() = 0;
};

In my C# project I was able to import and correctly use the first
method with:

[DllImport("vslib.dll")]
public static extern IntPtr DownloadIntra(string host, int port,
string user, string pwd, int canale);

For the second method:
I created my wrapper for the Connection class:

public abstract class Connection
{
public abstract int Disconnect();
public abstract int Authenticate();
public abstract int GetPort();
public abstract int Connected();
public abstract int ChannelCount();
public abstract string GetThreadID();
public abstract void FreeConnection();
public abstract void UsePing(bool use);
public abstract bool IsPingActive();
}

And Imported the method:
[DllImport("vslib.dll")]
public static extern Connection CreateConnection(string host, int
port, string user, string pwd);

The problem is:
In my project I create a Connection object with the CreateConnection
method:
Connection conn = CreateConnection("10.0.0.10", 80, "user", "pwd");
But after this, I noticed that the code executes the first method
(disconnect) instead of only create the Connection object, and then
the .net raises an exception
Nov 21 '07 #1
1 3431
The way that you are exposing the class is incorrect. You should be
using C++/CLI to create the managed wrapper in C++ and exposed as a .NET
assembly. The thing is, you wouldn't be able to export the function from
that assembly as well (AFAIK). You would have to expose it as a static
method on a class in that assembly.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"madbat87" <ma**************@gmail.comwrote in message
news:67**********************************@a28g2000 hsc.googlegroups.com...
Hi,
I'm trying to import code from a c++ unmanaged dll in a c# project.

In the dll there's a class named Connection, but the dll exposes only
2 methods (outside the class):
- HBITMAP DownloadIntra(char *host, int port, char *user, char *pwd,
int channel);
- Connection* CreateConnection(char *host, int port, char *user, char
*pwd);

My friend was able to import them in a C++ .Net project with:

- extern "C" __declspec(dllimport) HBITMAP __cdecl DownloadIntra(char
*host, int port, char *user, char *pwd, int channel);

- extern "C" __declspec(dllimport) Connection* __cdecl
CreateConnection(char *host, int port, char *user, char *pwd);

He created the Connection class wrapper with virtual methods:
class Connection
{
public:
virtual int __stdcall disconnect() = 0;
virtual int __stdcall authenticate() = 0;
virtual int __stdcall getPort() = 0;
virtual int __stdcall connected() = 0;
virtual int __stdcall getChannelsCount() = 0;
virtual char* __stdcall getThreadID() = 0;
virtual void __stdcall FreeConnection() = 0;
virtual void __stdcall usePing(bool ping) = 0;
virtual bool __stdcall isPingActive() = 0;
};

In my C# project I was able to import and correctly use the first
method with:

[DllImport("vslib.dll")]
public static extern IntPtr DownloadIntra(string host, int port,
string user, string pwd, int canale);

For the second method:
I created my wrapper for the Connection class:

public abstract class Connection
{
public abstract int Disconnect();
public abstract int Authenticate();
public abstract int GetPort();
public abstract int Connected();
public abstract int ChannelCount();
public abstract string GetThreadID();
public abstract void FreeConnection();
public abstract void UsePing(bool use);
public abstract bool IsPingActive();
}

And Imported the method:
[DllImport("vslib.dll")]
public static extern Connection CreateConnection(string host, int
port, string user, string pwd);

The problem is:
In my project I create a Connection object with the CreateConnection
method:
Connection conn = CreateConnection("10.0.0.10", 80, "user", "pwd");
But after this, I noticed that the code executes the first method
(disconnect) instead of only create the Connection object, and then
the .net raises an exception

Nov 21 '07 #2

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

Similar topics

0
by: Vinoth Kumar | last post by:
Hi I have created a unmanaged class in a header file UnManaged.h class UnManaged { UnManaged(){} ~UnManaged(){} void HelloWorld(){} };
9
by: Andrew Cumming | last post by:
I am having trouble using Platform Invoke to call C++ DLLs from C# windows applications. The DLLs seem to load, but I get run-time errors to the effect that the named entry points cannot be found....
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
3
by: Mark Urish | last post by:
Is there a way to import data from an unmanaged dll? For example, unmanaged.dll: __declspec(dllexport) int unmanagedErrno; managed.cs: class Managed { //this doesn't work:
5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
12
by: MuZZy | last post by:
HI, Say i have a class(on C++) which has an unmanaged STL member of STRMAP which is a define for map<string, string>. class CL { public CL(){mp = new STRMAP()}; public void Dispose(delete...
2
by: Paul Kenny | last post by:
Hi, I am trying to expose the functionality of an unmanaged C++ class to the other languages available in the .NET Framework. I have decided to do this by wrapping the unmanaged C++ class in a...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
6
by: William F. Kinsley | last post by:
I am thinking of porting an existing MFC application to MC++ and I have created a simple MFC application to test the environment. My sample MFC application is compilied with the /clr switch. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.