Error while implementing interface

G

Guest

These three lines work fine in c++.

hr = CoInitialize(NULL);
hr = CoCreateInstance(CLSID_WiaDevMgr, NULL, CLSCTX_ALL, IID_IWiaDevMgr,
(LPVOID *)&iDM);
hr = iDM->SelectDeviceDlg(NULL, 0, 0, NULL, &iRoot);

However, attempts to implement this in c# are proving difficult. Since
there is no idl or tlb for Windows Image Acquisition (WIA), I'm rolling my
own. Clearly I've done it incorrectly, but I can't spot it. I'm not looking
for someone to write the interface for me. Just for any general interop
tips, troubleshooting, or references.

I've included some code snippets in case something jumps out at someone.
The interfaces here are in wia.h in the platform sdk.

The error is that calls to SelectDeviceDlg keep returning
System.NullReferenceException:

Guid g = new Guid("a1f4e726-8cf1-11d1-bf92-0060081ed811");

IWiaDevMgr iw = (IWiaDevMgr) Activator.CreateInstance(
Type.GetTypeFromCLSID( g, true ) );
IWiaItem pRoot;
iw.SelectDeviceDlg(0, 1, 0, 0, out pRoot);

The (partial) interface listings are below. I've tried everything I can
think of. I've changed the parameter types to IntPtr, int, I've tried
MarshalAs, ref, nothing seems to work.


[Guid("4db1ad10-3391-11d2-9a33-00c04fa36145"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IWiaItem
{


etc


[ComVisible(true), ComImport,
Guid("5eb2502a-8cf1-11d1-bf92-0060081ed811"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IWiaDevMgr
{
[PreserveSig]
int EnumDeviceInfo(
long lFlag,
out IntPtr ppIEnum); // IEnumWIA_DEV_INFO **

[PreserveSig]
int CreateDevice(
[MarshalAs(UnmanagedType.BStr)] string bstrDeviceID,
IntPtr ppWiaItemRoot); // IWiaItem **

//[PreserveSig]
int SelectDeviceDlg(
/* [in] */ int hwndParent,
/* [in] */ long lDeviceType,
/* [in] */ long lFlags,
/* [out][in] */ int pbstrDeviceID, // Bstr *
[Out] out IWiaItem i3);


etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top