-_-不太明白,calss ss = new class();这个class从哪里来总得说明白吧! 如果是一个函数可以用如下方法把vc++的dll的导出函数fun1引入到c#中: [DllImport("MyDll.dll",EntryPoint="fun1")] public static extern int fun1(int a, int b); 但是怎样才能把vc++写的dll的类导入呢?antiking(Q狼E行)说的我不太明白......
请看函数void CommendWeapon(int target);他的输入是一个int型的target变量;再看结构体struct Weapon { int target; int FirstWeapon; int SecondWeapon; int ThirdWeapon; }; 在函数的具体实现中通过CommendWeapon这个函数就可以给出最后的结果保存在struct Weapon中。CommendWeapon这个函数是没有返回值的,他的作用是得到一个类中的结构体。那如果仅仅用[DllImport(...)]引入这个函数而没办法得到结果即那个结构体是不行的呀。不知我说清楚了没有?
to 另外我很想知道用VC++.NET真的会简单很多吗?那如果用VC++.NET会用到哪方面的知识呢?是不是C++托管还是C++/CLI?如果C#不是很复杂或是可能遇到更多的问题那我就继续弄下去了。因为你用vc.net调用dll要方便些,至少类似于指针方面的操作,要灵活多了。假如你如果dll的有.h以及.lib文件,就更方便了,直接在vc.net工程中引入.h,编译的时候加上.lib就可以了。
to 能否结合这个CommendWeapon函数和结构体Weapon给出个序列化的实例呢?这样方向明确一点。再次谢谢![StructLayout(LayoutKind.Sequential)] public struct Weapon { int target; int FirstWeapon; int SecondWeapon; int ThirdWeapon; }那么如上的Weapon就可以在调用dll函数中进行使用了。to void CommendWeapon(int target)的调用 如果不是class的成员函数的话,那么调用非常简单,如下: [DllImport(...)] public extern static void CommendWeapon( int target ) 但是如果是类的成员函数的话,就比较麻烦了,要做的操作大致如下: 首先需要创建一个类的对象,然后找到函数table,再找到相应的函数项,最后再调用。以上的操作在c#中很麻烦。
以这个简单的Dll的代码为例,在C#里应该怎样写那个虚拟的类呢? class _declspec(dllexport) MyClass { private: MyClass() { } public: static MyClass createInstance() { MyClass a; return a; }public: int result; void add(int a, int b) { result = a + b; } };
如果是一个函数可以用如下方法把vc++的dll的导出函数fun1引入到c#中:
[DllImport("MyDll.dll",EntryPoint="fun1")]
public static extern int fun1(int a, int b);
但是怎样才能把vc++写的dll的类导入呢?antiking(Q狼E行)说的我不太明白......
不知c#的平台调用是否就是==DllImport,是否只是引入非托管的函数而不能引入类,如果真的如此我就不能再看平台调用了。但是还有什么办法呢?
急呀!难道大家都没有遇到过或是考虑这样的问题吗?请大家给个法子呀!
>dumpbin -exports ExpertSystem.dllDump of file ExpertSystem.dllFile Type: DLL Section contains the following exports for ExpertSystem.dll 00000000 characteristics
44331D00 time date stamp Wed Apr 05 09:27:28 2006
0.00 version
1 ordinal base
25 number of functions
25 number of names ordinal hint RVA name 1 0 000011F9 ??0CExpert@@QAE@XZ
2 1 00001221 ??1CExpert@@QAE@XZ
3 2 00001294 ?AssertActionData@CExpert@@IAEXXZ
4 3 0000128F ?AssertInputData@CExpert@@IAEXXZ
5 4 00001235 ?ChooseClipsName@CExpert@@QAEXVCString@@@Z
6 5 0000101E ?CommendWeapon@CExpert@@QAEXH@Z
7 6 000010EB ?Execute@CExpert@@QAEXXZ
......
23 16 000011A4 ReportMemoryProblems
24 17 00001244 capturePrint
25 18 0000124E captureQuery Summary 1000 .data
1000 .idata
2000 .rdata
1000 .reloc
1000 .rsrc
22000 .text
#include "Clipsmfc.h"class _declspec(dllexport) CExpert
{
public:
void CommendWeapon(int target);
void GetWarning(int WarningID,float Data);
void ChooseClipsName(CString ClipsName);
void Execute();
struct Result
{
char name;
int action;
int target;
int weapon;
int method;
int direction;
int order;
};
struct Defense
{
int type;
int mode;
};
struct Input
{
PlaneState PlaneState_1;
PlaneState PlaneState_2;
PlaneState PlaneState_3;
PlaneState PlaneState_4;
Defense PlaneDefense;
};
struct Output
{
Result Result_1;
Result Result_2;
Result Result_3;
Result Result_4;
int maneuver;
};
struct Warning
{
int Weather;
float Wind_Direction;
float Horizontal_Wind_Speed;
float Static_Temperature;
float Static_Air_Pressure;
float Electromagnetism_State;
float Infrared_State;
int AlarmID;
float GASM_1;
float GASM_2;
float GASM_3;
float GASM_4;
};
struct WarningOut
{
int WarningID;
bool Result;
}; struct Weapon
{
int target;
int FirstWeapon;
int SecondWeapon;
int ThirdWeapon;
}; Input InputData;
Output OutputData;
Warning WarningData;
WarningOut WarningOutData;
Weapon WeaponCommended; CExpert();
~CExpert();
private:
CCLIPSWrap * m_pClips;
CStringArray m_standard;
CStringArray m_outNum;
CStringArray m_outDef;
CStringArray m_outAct;protected:
void ReadAction();
......
void WarnWeather(int Data);
BOOL RunClips();
void AssertInputData();
void ReleaseClips();
BOOL InitClips();
void ReadResult();
CString m_ClipsFileName;
};
{
int target;
int FirstWeapon;
int SecondWeapon;
int ThirdWeapon;
};
在函数的具体实现中通过CommendWeapon这个函数就可以给出最后的结果保存在struct Weapon中。CommendWeapon这个函数是没有返回值的,他的作用是得到一个类中的结构体。那如果仅仅用[DllImport(...)]引入这个函数而没办法得到结果即那个结构体是不行的呀。不知我说清楚了没有?
愚翁能否结合这个CommendWeapon函数和结构体Weapon给出个序列化的实例呢?这样方向明确一点。再次谢谢!
public struct Weapon
{
int target;
int FirstWeapon;
int SecondWeapon;
int ThirdWeapon;
}那么如上的Weapon就可以在调用dll函数中进行使用了。to void CommendWeapon(int target)的调用
如果不是class的成员函数的话,那么调用非常简单,如下:
[DllImport(...)]
public extern static void CommendWeapon( int target )
但是如果是类的成员函数的话,就比较麻烦了,要做的操作大致如下:
首先需要创建一个类的对象,然后找到函数table,再找到相应的函数项,最后再调用。以上的操作在c#中很麻烦。
既然DllImport可以调用Dll里的函数,而要想把dll类中的成员变量引进来比较麻烦。那我可不可以在Dll中稍加改造:我创建一个静态的成员函数,这个成员函数可以创建dll类的实例并且返回这个实例,那我在C#里可以用DllImport引入这个创建实例的静态函数就行了!这样在C#里就有了Dll中的类的实例,这样调用方法和成员变量不就都行了吗?不知这样对吗? 还有一个关键问题就是在C#里必须写一个和Dll中的类一样的类!这个是必须的吧,但是该怎么写呢?以下面这个例子为例:
class _declspec(dllexport) MyClass
{
private:
MyClass()
{
}
public:
static MyClass createInstance()
{
MyClass a;
return a;
}public:
int result;
void add(int a, int b)
{
result = a + b;
}
};
要么你用vc.net去引入.h和.lib做成类库供c#调用。
怎么办?