473,466 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

managed C# from unmanged C++ dll

I am new to C#, and I have an issue that I can't find the answer thru
googling or checking msdn. I want to run some c++ code from a dll in
C#. Here are snipits of the C++ header file.

struct ChartInfo
{
char symbol[12]; // symbol
int period; // period (PERIOD_*)
time_t start; // start of chart block
time_t end; // end of chart block
time_t timesign; // timestamp of existing
chart base
int mode; // request mode
};

struct RateInfo
{
time_t ctm; // rate time
int open; // open price:
11987=119.87
short high,low,close; // high,low,close shift
from open
double vol; // volume
};

class CManagerInterface
{
public:
//---- connection
virtual int __stdcall Connect(LPCSTR server) =0;
virtual int __stdcall Login(const int login,LPCSTR password)=0;
//---- chart bases
virtual RateInfo* __stdcall ChartRequest(const ChartInfo
*chart,time_t *timesign,int *total) =0;
};

inline code to load funcitons from dll.

typedef int (*MtManVersion_t)(void);
typedef int (*MtManCreate_t)(int version,CManagerInterface **man);

m_pfnManVersion=reinterpret_cast<MtManVersion_t>(: :GetProcAddress(m_lib,"MtManVersion"));
m_pfnManCreate
=reinterpret_cast<MtManCreate_t>(::GetProcAddress( m_lib,"MtManCreate"));

inline CManagerInterface* Create(const int version) const
{
CManagerInterface *man=NULL;
(*m_pfnManCreate)(version,&man);
return(man);
}

What I have writen in C#:

using System.Runtime.InteropServices;

namespace InterbankFX
{
public struct ChartInfo
{
char symbol[12]; // symbol
int period; // period (PERIOD_*)
time_t start; // start of chart block
time_t end; // end of chart block
time_t timesign; // timestamp of existing chart
base
int mode; // request mode
}

public struct RateInfo
{
time_t ctm; // rate time
int open; // open price: 11987=119.87
short high,low,close; // high,low,close shift
from open
double vol; // volume
}

public class Manager
{
public extern virtual int Connect(string server);
public extern virtual int Login(int login,string password);
public extern virtual RateInfo[] ChartRequest(ref ChartInfo chart,ref
time_t timesign,ref int total);
}//public class Manager

public class ManagerFactory
{
[DllImport("mtmanapi.dll",EntryPoint="MtManVersion" )]
public static extern int Version();
[DllImport("mtmanapi.dll",EntryPoint="MtManCreate")]
public static extern int CreateInteface(int version,ref Manager
manager);
}//public class ManagerFactory
}//namespace InterbankFX
I am hoping someone can help me fill in the gaps.

Steven

Nov 17 '05 #1
4 4977
One day, SJR3t2 wrote:
I am new to C#, and I have an issue that I can't find the answer thru
googling or checking msdn. I want to run some c++ code from a dll in
C#. Here are snipits of the C++ header file.

struct ChartInfo
{
char symbol[12]; // symbol
int period; // period (PERIOD_*)
time_t start; // start of chart block
time_t end; // end of chart block
time_t timesign; // timestamp of existing
chart base
int mode; // request mode
};

struct RateInfo
{
time_t ctm; // rate time
int open; // open price:
11987=119.87
short high,low,close; // high,low,close shift
from open
double vol; // volume
};

class CManagerInterface
{
public:
//---- connection
virtual int __stdcall Connect(LPCSTR server) =0;
virtual int __stdcall Login(const int login,LPCSTR password)=0;
//---- chart bases
virtual RateInfo* __stdcall ChartRequest(const ChartInfo
*chart,time_t *timesign,int *total) =0;
};

inline code to load funcitons from dll.

typedef int (*MtManVersion_t)(void);
typedef int (*MtManCreate_t)(int version,CManagerInterface **man);

m_pfnManVersion=reinterpret_cast<MtManVersion_t>(: :GetProcAddress(m_lib,"MtManVersion")); m_pfnManCreate
=reinterpret_cast<MtManCreate_t>(::GetProcAddress( m_lib,"MtManCreate"));

inline CManagerInterface* Create(const int version) const
{
CManagerInterface *man=NULL;
(*m_pfnManCreate)(version,&man);
return(man);
}

What I have writen in C#:

using System.Runtime.InteropServices;

namespace InterbankFX
{
public struct ChartInfo
{
char symbol[12]; // symbol
int period; // period (PERIOD_*)
time_t start; // start of chart block
time_t end; // end of chart block
time_t timesign; // timestamp of existing chart
base
int mode; // request mode
}

public struct RateInfo
{
time_t ctm; // rate time
int open; // open price: 11987=119.87
short high,low,close; // high,low,close shift
from open
double vol; // volume
}

public class Manager
{
public extern virtual int Connect(string server);
public extern virtual int Login(int login,string password);
public extern virtual RateInfo[] ChartRequest(ref ChartInfo chart,ref
time_t timesign,ref int total);
}//public class Manager

public class ManagerFactory
{
[DllImport("mtmanapi.dll",EntryPoint="MtManVersion" )]
public static extern int Version();
[DllImport("mtmanapi.dll",EntryPoint="MtManCreate")]
public static extern int CreateInteface(int version,ref Manager
manager);
}//public class ManagerFactory
}//namespace InterbankFX
I am hoping someone can help me fill in the gaps.

Steven


Hi Steven,

AFAIK, you can't do that with classes. You'd have to create complete
wrappers for each function, and create your own managed class, synonymous
to the unmanaged class.

--
Tom Spink
Nov 17 '05 #2
Okay, how do I go about writing a wrapper?

Steven

Nov 17 '05 #3
One day, SJR3t2 wrote:
Okay, how do I go about writing a wrapper?

Steven


Do you have access to the source code for the Unmanaged C++ application,
i.e. can you alter it, to make it easy on yourself?

--
Tom Spink
Nov 17 '05 #4
I don't have access to the source code, just the dll and the header
file.

Steven
http://blog.RetzNest.com

Nov 17 '05 #5

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

Similar topics

1
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
3
by: Mr Topom | last post by:
Hello, I have a C++ (unmanaged) class named K in a DLL. I create a managed C++ class named K in an other DLL that wrapped the unmanaged one. To refer the unmanaged one I use ::K and the...
2
by: Sandy | last post by:
I am confused about Unmanaged Code, How .Net Framework treate that code, What is the use of that. Thanks in advance Sandeep Chitode
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
3
by: DaTurk | last post by:
Hi, I'm implementing the Idisposable pattern and am wondering what the difference it between managed, and unmanaged resources? http://msdn2.microsoft.com/en-us/library/fs2xkftw.aspx in the...
1
by: mschuck | last post by:
Here is the scenario I'm trying to make work. I've got 2 managed C++ classes, each of which wrappes an unmanaged C++ class, kind of like so: __nogc class UnmanagedClassA { public: void...
6
by: B. | last post by:
my small project has two files, umg.cpp (unmanaged c++) and mged.cpp (MC++), and unmanged code will call managed code. However, I cannot debug from unmanged code into managed code. Can anyone help...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
5
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have a class that is writen in unmanaged pure native C++. This class files (h and cpp) are inserted to a managed C++ (VC++ 2005, C++/CLI) DLL compoenet. This DLL compoenet is used in a C#...
0
by: ElysianEagle | last post by:
Hi, I have a DLL written in C that creates a structure as a local variable, populates it, and then calls a function in managed c#. here is what im talking about: //-----unmanged code's header...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.