C#呼び出しC++ダイナミックリンクライブラリメソッド紹介-51 CTO.COM
8832 ワード
VC等でC#書き込みのCOMが呼び出されるとregasm/tlbでTLBファイルが生成する、tlbexpも利用できる.exeは、VC等にTLBファイルをロードする、C#でVC等で書かれたCOMを呼び出す場合にtlbimpを用いる.exe、プログラムを書いてデバッグしてもいいです.
C#がC++ダイナミックリンクライブラリを呼び出す方法について説明します.
システムを追加します.Runtime.InteropServicesネーミングスペース
COMの場合は静的関数で直接呼び出します.
COMを普通のDLLにしないと
直接使用できません
外部インタフェースはC++に1つしか追加できません.
C++ダイナミックリンクライブラリをC#で直接呼び出すことができます
これにより、C#がC++書き込みを呼び出すダイナミックリンクライブラリの問題が解決します.
引用源C#呼び出しC++ダイナミックリンクライブラリ方法紹介-51 CTO.COM
C#がC++ダイナミックリンクライブラリを呼び出す方法について説明します.
システムを追加します.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++に1つしか追加できません.
- 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++ダイナミックリンクライブラリを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;
- }
これにより、C#がC++書き込みを呼び出すダイナミックリンクライブラリの問題が解決します.
引用源C#呼び出しC++ダイナミックリンクライブラリ方法紹介-51 CTO.COM