Monday, December 29, 2014

Calling Win32 DLL from 64-bit WPF through ATL 32-bit Surrogate COM

(1) How to tell if a DLL is 32-bit or 64-bit

 dumpbin /headers MyServer.dll to tell by file Header x86 or x64

(2) Run 32-Bit COM in surrogate Procese so can be called from 64-bit WPF.(one kind of IPC)
       http://www.gfi.com/blog/32bit-object-64bit-environment/ 


 
  • Locate your COM object GUID under the HKey_Classes_Root\Wow6432Node\CLSID\[GUID]
  • Once located add a new REG_SZ (string) Value. Name should be AppID and data should be the same COM object GUID you have just searched for
  • Add a new key under HKey_Classes_Root\Wow6432Node\AppID\ The new key should be called the same as the com object GUID
  • Under the new key you just added, add a new REG_SZ (string) Value, and call it DllSurrogate. Leave the value empty
  • Create a new Key under HKey_Local_Machine\Software\Classes\AppID\ Again the new key should be called the same as the COM object’s GUID. No values are necessary to be added under this key.
  • (3) Step-by-step ALT 32-bit COM Creation (check to make sure project properties are x86) http://msdn.microsoft.com/en-us/library/dssw0ch4%28d=printer,v=vs.90%29.aspx http://msdn.microsoft.com/en-us/library/9yb4317s.aspx Note that C# COM is not usable in (2), especially HKey_Classes_Root\Wow6432Node\CLSID\[GUID] is not there. Also each build of ATL COM project will override this location and need to add back AppID={Guid} (4) A good overall review of 64-bit calling 32-bit using IPC http://blog.mattmags.com/2007/06/30/accessing-32-bit-dlls-from-64-bit-code/ (5) Dynamic Loading Win32 DLL to access its functions: #pragma hdrstop #pragma package(smart_init) // CObject1 HINSTANCE hMWXUSB=0; typedef int (__cdecl *MYPROC)(LPWSTR); typedef unsigned char (ACCEPT_LED)(int data); ACCEPT_LED *lpacceptled; typedef unsigned char (OPEN_USB)(void); OPEN_USB *lpopenusb; STDMETHODIMP CObject1::get_GetANum(SHORT* pVal) { hMWXUSB=LoadLibrary(L"MWXUSB.DLL"); MYPROC Accept_LED; //Accept_LED = (MYPROC) GetProcAddress(hMWXUSB, "Accept_LED"); if( (hMWXUSB = LoadLibrary(L"MWXUSB.DLL")) == NULL ) { *pVal=-5; return S_OK; } if( (lpopenusb=(OPEN_USB*)GetProcAddress(hMWXUSB,"Open_USB" ))==NULL ) { *pVal=-6; return S_OK; } unsigned char ucOpen; ucOpen = (*lpopenusb)(); if( (lpacceptled=(ACCEPT_LED*)GetProcAddress(hMWXUSB,"Accept_LED" ))==NULL ) { *pVal=-1105; return S_OK; } unsigned char ch; ch = (*lpacceptled)(1); if(ch) { *pVal=-2105; return S_OK; } *pVal=105; return S_OK; }

    Saturday, December 27, 2014

    Call 3rd party DLL from ATL COM

    (1) use tool to generate def and .lib from DLL ( assuming no header file)
        http://purefractalsolutions.com/show.php?a=utils/expdef
    
    (2) VC++ project linker-->input additional dependency
    
    (3) declare and use in CPP
    
    extern "C"   unsigned char Open_USB();
    extern "C"   unsigned char Accept_LED(int value);
    extern "C"  void Set_Callback(void* pAny);
    
    void(__stdcall*p)(int);  // calling convention matching far pascal, for most c function
    
    //void __cdecl* p;  // ptrAny
    
    
    static void __stdcall CB1(int value)  {..} instance function would hast this->* so callback must use static to remove this.
    
     p = &CB1;
     ::Open_USB();
     ::Accept_LED(0);
     ::Set_Callback(p);
    
    again cannot use __cdecl must use __stdcall. It turns out Calling Conversion even affect [DllImport] when re-target .net 4.0 from .net 4.0, must specify CDecl in DllImports
    

    Thursday, December 25, 2014

    Manually add Connection Point support after ATL Simple object wizard already run

    After running ATL COM Simple object wizard without check "Connection Points", manual steps are need to add this support.
    
    (1) myserver.h---IConnectionPointContainer/Impl, COM_MAP, IEvents
    
    #include "ATLProject1_i.h"
    #include "_IMyServerEvents_CP.h"
    
    
    class ATL_NO_VTABLE CMyServer :
     public CComObjectRootEx<CComSingleThreadModel>,
     public CComCoClass<CMyServer, &CLSID_MyServer>,
     public IConnectionPointContainerImpl<CMyServer>,
     public CProxy_IMyServerEvents<CMyServer>,
     public IDispatchImpl<IMyServer, &IID_IMyServer, &LIBID_ATLProject1Lib, /*wMajor =*/ 1, /*wMinor =*/ 0>
    {
    
    
    BEGIN_COM_MAP(CMyServer)
     COM_INTERFACE_ENTRY(IMyServer)
     COM_INTERFACE_ENTRY(IDispatch)
     COM_INTERFACE_ENTRY(IConnectionPointContainer)
    END_COM_MAP()
    
    BEGIN_CONNECTION_POINT_MAP(CMyServer)
     CONNECTION_POINT_ENTRY(__uuidof(_IMyServerEvents))
    END_CONNECTION_POINT_MAP()
    
    (2)   .\atlproject1\atlproject1\atlproject1.vcxproj.filters -- need one include
    
        <ClInclude Include="ATLProject1_i.h">
          <Filter>Generated Files</Filter>
        </ClInclude>
        <ClInclude Include="_IMyServerEvents_CP.h">
          <Filter>Header Files</Filter>
        </ClInclude>
        <ClInclude Include="MyServer.h">
          <Filter>Header Files</Filter>
        </ClInclude>
      </ItemGroup>
    
    
    (3).\atlproject1\atlproject1\atlproject1.vcxproj --- include .h for compiler
     
    
      <ItemGroup>
        <ClInclude Include="ATLProject1_i.h" />
        <ClInclude Include="dllmain.h" />
        <ClInclude Include="MyServer.h" />
        <ClInclude Include="Resource.h" />
        <ClInclude Include="stdafx.h" />
        <ClInclude Include="targetver.h" />
        <ClInclude Include="xdlldata.h" />
        <ClInclude Include="_IMyServerEvents_CP.h" />
      </ItemGroup>
    
    
    
    (4)   .\atlproject1\atlproject1\atlproject1.idl  -- disinterface and coclass
    
    library ATLProject1Lib
    {
     importlib("stdole2.tlb");
     [
      uuid(1D071C81-6C67-458F-9B35-992DBAE162D8) // one of these are duplicate 
      uuid(118E250A-B298-4749-90D8-36C764237BA4)  
     ]
     dispinterface _IMyServerEvents
     {
      properties:
      methods:
     };
     [
      uuid(48A5B08F-ED8F-4F1F-8344-5F7C913FB895)  
     ]
     coclass MyServer
     {
      [default] interface IMyServer;
      [default, source] dispinterface _IMyServerEvents;
     };
    };
    
    
    Notes:
    (n1) to user Connection point, Add Method to _IMyServerEvents (Class View Context Menu) e.g. signature CB(int data). 
    Then Add->Connection point onto CMyServer Class (again Class View Ctx Menu) select _IMyS erverEvents in
     Source Interface List. Now there will be a function Fire_CB to fire COM Event into WPF client
    
    (n2) MyServer.rgs need update ForceRemove {dispinterface _IMyServerEvents GUI in IDL} = s 'MyServer Class'
    
    (n3) Content of  [project dir]\_IMyServerEvents_CP.h
    #pragma once
    
    using namespace ATL;
    
    template 
    class CProxy_IMyServerEvents : public IConnectionPointImpl
    {
     // WARNING: This class may be regenerated by the wizard
    public:
    };