managed C# from unmanged C++ dll

S

SJR3t2

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
 
T

Tom Spink

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 said:
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.
 
T

Tom Spink

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?
 

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