1

我有一个类型库项目“MyLib”,我必须在其中添加一个新方法,如下所示。从我的 C# 客户端应用程序调用时,它应该IntPtr作为参数。

以下是“MyLib”项目中方法的 IDL 定义:

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

谁能告诉我在我的 IDL 文件中我应该为那个特定的参数使用什么类型?

以下是我的 C# 客户端项目代码:

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);
}

当我HANDLE在 IDL 定义中使用时,会引发以下异常:

无法将“System.__ComObject”类型的 COM 对象转换为接口类型...

4

1 回答 1

0

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.

于 2013-01-31T10:38:21.413 回答