C++调用C#接口dll,对dll进行注册

来源:互联网 发布:linux xargs命令 编辑:程序博客网 时间:2024/06/04 23:36

经常会有从一个应用程序同步数据到另一个应用程序的功能。比如说应用A是C++写的,所要同步的程序是用C#完成的。应用B提供了接口供第三方应用同步,接口是用C#编程的dll,那么如何成功的调用dll呢?在调用之前必须的对dll进行注册。

  C# DLL为:testSync.dll

 注册testSync.dll

BOOL RegisterSyncDll(){        CONST TCHAR*  Dll_NAME= _T("testSync.dll");       //在exe同目录下获取dll路径CString strAppExePath;AfxGetModuleFileName(AfxGetInstanceHandle(), strAppExePath);strAppExePath = strAppExePath.Left(strAppExePath.ReverseFind(_T('\\')) + 1);CString strDllPath = strAppExePath + Dll_NAME;       //获取.net framework安装路径TCHAR szSystemPath[260] = _T("");SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, 0, szSystemPath);CString strSyntemPath = szSystemPath;CString str2RegasmPath = strSyntemPath + _T("\\Microsoft.NET\\Framework\\v2.0.50727\\regasm.exe");CString str4RegasmPath = strSyntemPath + _T("\\Microsoft.NET\\Framework\\v4.0.30319\\regasm.exe");       //判断当前使用的.net framwork 版本.判断方法各不相同CString strRegasmExe;if (GetFileAttributes(str2RegasmPath) != INVALID_FILE_ATTRIBUTES){strRegasmExe = str2RegasmPath;}else if (GetFileAttributes(str4RegasmPath) != INVALID_FILE_ATTRIBUTES){strRegasmExe = str4RegasmPath;}if (strRegasmExe.IsEmpty()){           CString strNetFrameWork = RT_LoadString(_T("Message"), _T("IDS_INSTALL_NETFRAMEWOK"), NULL, _T( "Please install .net framework 2.0 or later" );           AfxMessageBox(strNetFrameWork);           return FALSE;}       //根据版本得出CmdLineCString strCmdLine = _T("\"") + strRegasmExe + _T("\" /c \"") + strDllPath + _T("\"");for (int i = 0; i < 2; i++){STARTUPINFO si;RtlZeroMemory(&si, sizeof(si) );si.cb = sizeof(si);si.dwFlags = STARTF_USESHOWWINDOW;si.wShowWindow = SW_HIDE;PROCESS_INFORMATION pi;RtlZeroMemory( &pi, sizeof(pi) );if (!CreateProcess( NULL,   // No module name (use command line)(LPTSTR)(LPCTSTR)strCmdLine,        // Command lineNULL,           // Process handle not inheritableNULL,           // Thread handle not inheritableFALSE,          // Set handle inheritance to FALSE0,              // No creation flagsNULL,           // Use parent's environment blockNULL,           // Use parent's starting directory &si,            // Pointer to STARTUPINFO structure&pi )           // Pointer to PROCESS_INFORMATION structure) {return FALSE;}WaitForSingleObject(pi.hProcess, INFINITE );CloseHandle(pi.hProcess );CloseHandle(pi.hThread );}return TRUE;}


  

0 0