日期:2014-05-18  浏览次数:20665 次

搞了一天了 郁闷啊 高手门帮个忙把
我是用C#调用C++写的一个DLL
  HC.DLL   就一个函数   int   GetHashCode(WCHAR   *pFullPathFileName,   WCHAR   pOutHashCode);

我在C#中是这么定义的
  public   static   extern   int   GetHashCode([In,   MarshalAs(UnmanagedType.LPWStr)]   string   lpFullPathFileName,   [MarshalAs(UnmanagedType.LPWStr)]     ref   String   lpOutHashCode);

可是老报异常     我要是把第二个参数的REF去掉就没事了       可是第二个参数是我要得到数值的     请高手门帮帮忙吧

------解决方案--------------------
改成out试试
------解决方案--------------------
“PInvoke 签名与非托管的目标签名不匹配”
这样不对,增加了REF说明参数类型不匹配。
是不是应该:int GetHashCode(WCHAR *pFullPathFileName, WCHAR *pOutHashCode);

------解决方案--------------------
给你一个例子:

原型:

This function reads a BYTE/WORD/DWORD value from an I/O port.

bool _stdcall GetPortVal(
WORD wPortAddr,
PDWORD pdwPortVal,
BYTE bSize
);
Parameters
wPortAddr
[in] I/O port address

pdwPortVal
[out] Pointer to a DWORD variable that receives the value obtained from the port.

bSize
[in] Number of bytes to read.
Can be 1 (BYTE), 2 (WORD) or 4 (DWORD).
Return Values
If the function succeeds, the return value is true. Otherwise, the function returns false.

C#中调用的

/// <summary>
/// This function reads a BYTE/WORD/DWORD value from an I/O port.
/// </summary>
/// <param name= "wPortAddr "> [in] I/O port address </param>
/// <param name= "pdwPortVal "> [out] Pointer to a DWORD variable that receives the value obtained from the port. </param>
/// <param name= "bSize "> [in] Number of bytes to read. Can be 1 (BYTE), 2 (WORD) or 4 (DWORD). </param>
/// <returns> If the function succeeds, the return value is true. Otherwise, the function returns false. </returns>
[DllImport( "WinIo.dll ", EntryPoint = "GetPortVal ")]
private static extern bool GetPortVal(UInt16 wPortAddr, [MarshalAs(UnmanagedType.U4)] out UInt32 pdwPortVal, byte bSize);