c#中如何调用vc++写的动态链接库
2008-09-28 09:26
801 查看
当VC等调用C#写的COM时,用regasm /tlb生成TLB文件,也可用tlbexp.exe,在VC等中加载TLB文件,当用C#调用VC等写的COM时,用tlbimp.exe,你可以写一个程序调试一下
添加System.Runtime.InteropServices命名空间
如是COM就直接用静态函数调用:
public static int GetNum(
int lFileSeqNo,
string sExtType,
string sExtNumber,
string sFormID,
string sOperationDate,
string sSystemRegistDate,
out int lCount,
out int lErrorType,
out int lErrorCode)
{
int iRet;
WOBCom.ObjClass obj = new WOBCom.ObjClass();
iRet = obj.GetNum(
lFileSeqNo,
sExtType,
sExtNumber,
sFormID,
sOperationDate,
sSystemRegistDate,
out lCount,
out lErrorType,
out lErrorCode);
return iRet;
}
如不使COM是普通的DLL
不能直接用
只能在C++中加一个对外的接口:
extern "C" __declspec(dllexport) WOExtConRegObj* OutGetObjConstructor();
extern "C" __declspec(dllexport) void OutGetObjDestructor(WOExtConRegObj* outGetObj);
extern "C" __declspec(dllexport) long SelectDummyRecord(long *lErrorType,
long *lErrorCode,
WOExtConRegObj* outGetObj);
//
extern "C" __declspec(dllexport) WOExtConRegObj* OutGetObjConstructor()
{
WOExtConRegObj* outGetObj = new WOExtConRegObj();
return outGetObj;
}
extern "C" __declspec(dllexport) void OutGetObjDestructor(WOExtConRegObj* outGetObj)
{
delete outGetObj;
}
extern "C" __declspec(dllexport) long SelectDummyRecord(long *lErrorType,
long *lErrorCode,
WOExtConRegObj* outGetObj)
{
return outGetObj->SelectDummyRecord(lErrorType,
lErrorCode);
}
C#就可直接调用了
[DllImport("XXX.dll",
EntryPoint="SelectDummyRecord",
ExactSpelling=false,
CallingConvention=CallingConvention.Cdecl)]
private static extern int SelectDummyRecord(
out int lErrorType,
out int lErrorCode,
int outGetObj);
/// <summary>
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="lErrorType"></param>
/// <param name="lErrorCode"></param>
/// <returns></returns>
public int SelectDummyRecord(
out int lErrorType,
out int lErrorCode)
{
int intRtn;
intRtn = SelectDummyRecord(
out lErrorType,
out lErrorCode,
m_OutGetObj);
return intRtn;
}
添加System.Runtime.InteropServices命名空间
如是COM就直接用静态函数调用:
public static int GetNum(
int lFileSeqNo,
string sExtType,
string sExtNumber,
string sFormID,
string sOperationDate,
string sSystemRegistDate,
out int lCount,
out int lErrorType,
out int lErrorCode)
{
int iRet;
WOBCom.ObjClass obj = new WOBCom.ObjClass();
iRet = obj.GetNum(
lFileSeqNo,
sExtType,
sExtNumber,
sFormID,
sOperationDate,
sSystemRegistDate,
out lCount,
out lErrorType,
out lErrorCode);
return iRet;
}
如不使COM是普通的DLL
不能直接用
只能在C++中加一个对外的接口:
extern "C" __declspec(dllexport) WOExtConRegObj* OutGetObjConstructor();
extern "C" __declspec(dllexport) void OutGetObjDestructor(WOExtConRegObj* outGetObj);
extern "C" __declspec(dllexport) long SelectDummyRecord(long *lErrorType,
long *lErrorCode,
WOExtConRegObj* outGetObj);
//
extern "C" __declspec(dllexport) WOExtConRegObj* OutGetObjConstructor()
{
WOExtConRegObj* outGetObj = new WOExtConRegObj();
return outGetObj;
}
extern "C" __declspec(dllexport) void OutGetObjDestructor(WOExtConRegObj* outGetObj)
{
delete outGetObj;
}
extern "C" __declspec(dllexport) long SelectDummyRecord(long *lErrorType,
long *lErrorCode,
WOExtConRegObj* outGetObj)
{
return outGetObj->SelectDummyRecord(lErrorType,
lErrorCode);
}
C#就可直接调用了
[DllImport("XXX.dll",
EntryPoint="SelectDummyRecord",
ExactSpelling=false,
CallingConvention=CallingConvention.Cdecl)]
private static extern int SelectDummyRecord(
out int lErrorType,
out int lErrorCode,
int outGetObj);
/// <summary>
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="lErrorType"></param>
/// <param name="lErrorCode"></param>
/// <returns></returns>
public int SelectDummyRecord(
out int lErrorType,
out int lErrorCode)
{
int intRtn;
intRtn = SelectDummyRecord(
out lErrorType,
out lErrorCode,
m_OutGetObj);
return intRtn;
}
相关文章推荐
- VC调用Delphi制作的动态链接库如何互相传递字符串
- VC++ 6.0如何创建与调用动态链接库
- VC++ 6.0如何创建与调用动态链接库
- VC++ 6.0如何创建与调用动态链接库
- C#中如何使用VC++创建的dll动态链接库的返回参数
- Delphi中如何调用VC++创建的动态链接库
- VC调用Delphi制作的动态链接库如何互相传递字符串
- VC++如何创建与调用动态链接库
- VC++ 6.0如何创建与调用动态链接库
- VC调用Delphi制作的动态链接库如何互相传递字符串
- 【转】C#生成dll文件:如何用C#编译生成dll文件即动态链接库文件供其它程序调用
- VC++ 6.0如何创建与调用动态链接库
- VC调用Delphi制作的动态链接库如何互相传递字符串
- 将VC++动态链接库封装成C#可以用的动态链接库步骤
- VC调用MatLab生成的动态链接库
- C# 中动态调用C++动态链接
- Oracle如何调用Windows动态链接库
- Qt调用VC++生成的动态链接库
- 在Delphi中调用VC++创建的动态链接库的实例
- 利用VC调用动态链接库中的函数