سؤال

I'm trying to use a .NET COM-dll which is part of the SDK for a stepper motor control interface. The SDK provides a type library, which I imported into Delphi via 'Import Component > Import a Type Library'.

Although I know some basics about COM programming, I struggle to get the interface working. The problem seems to be that the CoClass as well as the OLE Server Proxy only expose an empty interface, i.e. an interface without any methods (_ComMotorCommands). Casting from there to the actual IComMotorCommands yields an Interface not supported error.

// *********************************************************************//
// Interface: IComMotorCommands
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {0D4E3A3F-0C07-438B-9F6F-CC1968408CFC}
// *********************************************************************//
  IComMotorCommands = interface(IDispatch)
    ['{0D4E3A3F-0C07-438B-9F6F-CC1968408CFC}']
    function Get_ErrorFlag: WordBool; safecall;
    function Get_ErrorMessageString: WideString; safecall;    
    function Get_ErrorNumber: Integer; safecall;
    ...
  end;

...

// *********************************************************************//
// Interface: _ComMotorCommands
// Flags:     (4432) Hidden Dual OleAutomation Dispatchable
// GUID:      {5AB3EB9E-E847-3133-95DC-4DDC7870C02A}
// *********************************************************************//
  _ComMotorCommands = interface(IDispatch)
    ['{5AB3EB9E-E847-3133-95DC-4DDC7870C02A}']
  end;

...

  CoComMotorCommands = class
    class function Create: _ComMotorCommands;
    class function CreateRemote(const MachineName: string): _ComMotorCommands;
  end;

I have also tried to create the interface manually via

CreateComObject(CLASS_ComMotorCommands) as IComMotorCommandsDisp

which itself does not throw an exception. Even setting some connection properties such as port and baud rate seem to work. But as soon as I try to query the motor status, I get

'BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)',

mind you, not as an exception in my application, but via the error reporting mechanism of the interface itself (Get_ErrorMessageString).

This error usually indicates a bitness-conflict between x86 and x64 code. But my platform target is x86, as is the SDK. I'm running Delphi XE3 on Windows 7 x64, but the same error also appeared on a 32bit machine i tried, which does not make any sense to me.

The SDK also offers a couple of Visual Studio examples for VB, C++ and C#. But all of them use the Visual Studio-way of calling the interface and are of no help to me. (There you reference the TLB directly, simply declare a variable of type 'ComMotorCommands' and can then access all interface methods freely.)

I will provide full files on request via email.

I'm in over my head here, so any help on this would be hugely appreciated!

هل كانت مفيدة؟

المحلول

The problem most likely is an attempt to load a 64 bit module. Debug this with Dependency Walker in profile mode.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top