我正在尝试从C#调用一个非托管方法,它返回一个指向结构的指针。
该方法似乎返回一个值但是当我使用指针调用Marshal.PtrToStructure时,我得到一个访问冲突错误。
系统是Windows Server 2012 r2,我的代码和互操作编译为64位。
以下是IDL的签名:
typedef struct _tagCCatConfigInfo {
DWORD dwCCatConfigInfoFlags;
DWORD dwEnable;
DWORD dwCatFlags;
LPSTR pszRoutingType;
LPSTR pszBindDomain;
LPSTR pszUser;
LPSTR pszPassword;
LPSTR pszBindType;
LPSTR pszSchemaType;
LPSTR pszHost;
LPSTR pszNamingContext;
LPSTR pszDefaultDomain;
DWORD dwPort;
ISMTPServer *pISMTPServer;
ICategorizerDomainInfo *pIDomainInfo;
DWORD dwVirtualServerID;
} CCATCONFIGINFO, *PCCATCONFIGINFO;
interface ICategorizerParameters : IUnknown
{
HRESULT GetDSParameterA(
[in] DWORD dwDSParameter,
[out] LPSTR *ppszValue);
HRESULT SetDSParameterA(
[in] DWORD dwDSParameter,
[in, unique] LPCSTR pszValue);
HRESULT RequestAttributeA(
[in, unique] LPCSTR pszName);
HRESULT GetAllAttributes(
[out] LPSTR **prgszAllAttributes);
HRESULT ReserveICatItemPropIds(
[in] DWORD dwNumPropIdsRequested,
[out] DWORD *pdwBeginningPropId);
HRESULT ReserveICatListResolvePropIds(
[in] DWORD dwNumPropIdsRequested,
[out] DWORD *pdwBeginningPropId);
HRESULT GetCCatConfigInfo(
[out] PCCATCONFIGINFO *ppCCatConfigInfo);od
};
管理对手:
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct _tagCCatConfigInfo
{
public uint dwCCatConfigInfoFlags;
public uint dwEnable;
public uint dwCatFlags;
[MarshalAs(UnmanagedType.LPStr)]
public string pszRoutingType;
[MarshalAs(UnmanagedType.LPStr)]
public string pszBindDomain;
[MarshalAs(UnmanagedType.LPStr)]
public string pszUser;
[MarshalAs(UnmanagedType.LPStr)]
public string pszPassword;
[MarshalAs(UnmanagedType.LPStr)]
public string pszBindType;
[MarshalAs(UnmanagedType.LPStr)]
public string pszSchemaType;
[MarshalAs(UnmanagedType.LPStr)]
public string pszHost;
[MarshalAs(UnmanagedType.LPStr)]
public string pszNamingContext;
[MarshalAs(UnmanagedType.LPStr)]
public string pszDefaultDomain;
public uint dwPort;
[MarshalAs(UnmanagedType.Interface)]
public ISMTPServer pISMTPServer;
[MarshalAs(UnmanagedType.Interface)]
public ICategorizerDomainInfo pIDomainInfo;
public uint dwVirtualServerID;
}
[ComConversionLoss, Guid("86F9DA7B-EB6E-11D1-9DF3-00C04FA322BA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
public interface ICategorizerParameters
{
....
....
....
[MethodImpl(MethodImplOptions.InternalCall)]
void GetCCatConfigInfo([Out] ref IntPtr ppCCatConfigInfo);
}
我的托管呼叫代码:
void IMailTransportCategorize.ExpandItem(ICategorizerParameters pICatParms, ICategorizerItem pICatItem, IMailTransportNotify pINotify, IntPtr pvNotifyContext)
{
IntPtr ppCCatConfigInfo = IntPtr.Zero;
try
{
pICatParms.GetCCatConfigInfo(out ppCCatConfigInfo);
_tagCCatConfigInfo theStruct = (_tagCCatConfigInfo)Marshal.PtrToStructure(ppCCatConfigInfo, typeof(_tagCCatConfigInfo)); <-- Access violation
}
catch(exception ex)
}
Debug.WriteLine("Exception: {0}", ex.Message);
}
finally
{
if (null != pICatParms) { Marshal.ReleaseComObject(pICatParms); }
if (null != pICatItem) { Marshal.ReleaseComObject(pICatItem); }
if (null != pINotify) { Marshal.ReleaseComObject(pINotify); }
}
}
这是我获取访问权限的地方
tagCCatConfigInfo theStruct = (_tagCCatConfigInfo)Marshal.PtrToStructure(ppCCatConfigInfo, typeof(_tagCCatConfigInfo))
系统崩溃,输出窗口显示 - 类型&#39; System.AccessViolationException&#39;的第一次机会异常。发生在mscorlib.dll中 错误日志没有任何异常条目,SMTP服务停止。
有什么想法吗?
由于 Al