1
votes

I have a Type Library Project "MyLib" where I have to add a new method as shown below. When called from my C# client application it should take IntPtr as an argument.

Below is the IDL definition of the method inside "MyLib" project:

[id(9), helpstring("method PrintFile"), local] 
HRESULT PrintFile([in] HANDLE pDevMode);

Can anyone please tell me what type I should use for that perticular argument in my IDL file?

Below are my C# Client Project codes:

PrintDialog PrntDlg = new PrintDialog();
PrintDocument printDocument = new PrintDocument();

printDocument.DocumentName = "filename";

PrntDlg.Document = printDocument;

PrntDlg.AllowSelection = true;
PrntDlg.AllowSomePages = true;

if (PrntDlg.ShowDialog() == DialogResult.OK)
{                  
    IntPtr PDevMode = PrntDlg.PrinterSettings.GetHdevmode();
    MyLib.PrintFile(PDevMode);
}

When I am using HANDLE inside the IDL definition the following exception get's thrown:

Unable to cast COM object of type 'System.__ComObject' to interface type ...

1

1 Answers

0
votes

You can simply use void * (or LPVOID) to pass an unformatted pointer to the unmanaged COM interface. Take a look at this table.

However creating pointers in C# to make the client fit the library is often a sign of bad API design. You should create an object or wrapper that fit's the purpose you are trying to achive and pass an instance of that object to the method. This increases compatibility and makes it easier to write clients.