Note that there are some explanatory texts on larger screens.

plurals
  1. POCoCreateInstance() -858993460 (in comip.h) C++
    text
    copied!<p>I will spend a little time explaining my project stucture:</p> <p><strong>There are three dlls:</strong></p> <ol> <li>mclController.dll - a third party dll written in C# to control the hardware..</li> <li>MCLWrapper.dll - I wrote this ll in C# such that it will be working as a COM to expose the mclControl.dll to a native C++ dll.</li> <li>ThorDetectorSwitch.dll - I wrote this dll with native C++.</li> </ol> <p><strong>Structure:</strong></p> <ul> <li>The ThorDetectorSwitch.dll calls the MCLWrapper.dll which wraps mclController.dll.</li> <li>I am implementing a small testing console application in C++, TDSTest.exe to call ThorDetecttorSwitch.dll.</li> </ul> <p>So it basically works like this: <em>TDSTest.exe -> ThorDetectorSwitch.dll -> MCLWrapper -> mclController.dll</em></p> <p><strong>Some code:</strong></p> <p>-How TDSTest.exe (Windows console application, built with x64 configuration) calls ThorDetectorSwitch.dll:</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;math.h&gt; #include &lt;windows.h&gt; #include "TDSTest.h" typedef long (*TDSFindDevices)(long&amp;); typedef long (*TDSGetParam)(const long, double&amp;); typedef long (*TDSTeardownDevice)(); typedef long (*TDSStartPosition)(); using namespace std; int _tmain(int argc, _TCHAR* argv[]) { if (argc &lt; 2) { cout&lt;&lt;"This is ThorDetecttorSwitch test program."&lt;&lt;endl; return 1; } HINSTANCE hInst = LoadLibrary(_T(".\\Modules_Native\\ThorDetectorSwitch.dll")); if( hInst == NULL ) { DWORD err = GetLastError(); cout&lt;&lt;"Error loading ThorDetectorSwitch.dll. Program exiting..."&lt;&lt;endl; return 1; } } </code></pre> <p>-Constructor of the ThorDetectorSwitch.dll <strong>EDITTED!</strong> on 06/15/2013, Central Time 19:41</p> <pre><code>ThorDetectorSwitch::ThorDetectorSwitch() :_mcSwitch(ComHelper(__uuidof(MCLControlClass))) { CoInitialize(NULL); MCLWrapper::MCLControlPtr mclSmartPtr; HRESULT hr = CoCreateInstance(__uuidof(MCLWrapper::MCLControlClass), NULL, CLSCTX_ALL, __uuidof(MCLWrapper::MCLControl), (void**)&amp;mclSmartPtr); // program breaks right here!!! _mcSwticth = mclSmartPtr; _A = WstringToBSTR(L"A"); _B = WstringToBSTR(L"B"); _C = WstringToBSTR(L"C"); _D = WstringToBSTR(L"D"); _deviceDetected = FALSE; } </code></pre> <p>The MCLWrapper that makes a COM object</p> <pre><code>// C# COM wrapper using System; using System.Collections.Generic; using System.Linq; using System.Text; using mcl_RF_Switch_Controller64; using System.Runtime.InteropServices; // for function reference see miniCircuit RF controller manual namespace MCLWrapper { [Guid("7C312A7C-2E77-4de7-A76F-990F268AB818")] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface MCLControl { [DispId(1)] void Connect(string SerialNumber); [DispId(2)] void Set_Switch(string SwitchName, int Val); [DispId(3)] void Set_SwitchesPort(byte binVal); [DispId(4)] void GetSwitchesStatus(int statusRet); [DispId(5)] void Disconnect(); }; [Guid("BEC33A1D-BB98-4332-B326-92D480ECC246"), ClassInterface(ClassInterfaceType.None)] public class MCLControlClass : MCLControl { private USB_RF_SwitchBox _sb = new USB_RF_SwitchBox(); public void Connect(string SerialNumber) { _sb.Connect(ref SerialNumber); } public void Set_Switch(string SwitchName, int Val) { _sb.Set_Switch(ref SwitchName, ref Val); } public void Set_SwitchesPort(byte binVal) { _sb.Set_SwitchesPort(ref binVal); } public void GetSwitchesStatus(int statusRet) { _sb.GetSwitchesStatus(ref statusRet); } public void Disconnect() { _sb.Disconnect(); } } } </code></pre> <p><strong>My Problem:</strong></p> <p>When the TDSTest is executed, it first hits </p> <pre><code>HINSTANCE hInst = LoadLibrary(_T(".\\Modules_Native\\ThorDetectorSwitch.dll")); </code></pre> <p>then it breaks at: <code>hr = CoCreateInstance(......)</code> in the ThorDetectorSwitch.cpp</p> <p><code>hr = -858993460</code> is the return;</p> <p><strong>A few additionals</strong></p> <ol> <li>I am kept be told it was because <code>CoInitialized()</code> did not get called, and that is the reason, but I feel that is not the reason because this ThorDetectorSwitch.dll works perfectly fine with another application, and I beleive I have called <code>CoInitialized()</code> in my code.</li> <li>I have registered my MCLWrapper.dll with <code>regasm MCLWrapper.dll /tlb:MCLWrapper.tlb /codebase</code></li> <li>Debugger output: "Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initalization function since doing so can cause the application of hang."</li> </ol> <p>So right now I have no idea what direction I should go, and I have been struglling with this problem for days. So I really hope someone can give some pointers for me. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload