[英]Buffer overrun in ATL COM dll
I am trying to create a COM dll using ATL that matches another one I had originally had in C#. 我正在尝试使用ATL创建一个与我最初在C#中拥有的另一个匹配的COM dll。 This dll is normally called by a third party dll that I do not have the source code for so I wanted the UUIDs to be the same as well as the progID. 该dll通常由第三方dll调用,而我没有此DLL的源代码,因此我希望UUID与progID相同。 I also have a test application that calls methods in my dll and that is how I have been testing it. 我也有一个测试应用程序,该应用程序在dll中调用方法,这就是我一直对其进行测试的方式。 The interface defined by the test application is: 测试应用程序定义的接口是:
STDMETHOD(GetSwitchCriteria)(double* intensity, double* minMass, double* maxMass,
BOOL* selectIntensity, long* numOfDepCycles) = 0;
STDMETHOD(GetChargeStateParam)(short* minCharge, short* maxCharge, BOOL* doChargeState) = 0;
STDMETHOD(GetInclusionList)(double* intensity, double* theList, short* numOfItems) = 0;
STDMETHOD(GetExclusionList)(long* exRTWindow, double* theMassList, long* theRTList,
short* numOfItems) = 0;
STDMETHOD(GetOtherCriteria)(long* smartFilterTime, double* isoExclusionWin,
double* massTolerance, BOOL* isPPM) = 0;
STDMETHOD(GetIsotopeMatchParam)(double* theMassList, double* theAbundanceList,
short* numOfItems, double* abTolerance, double* maTolerance) = 0;
// called at the start of each IDA run
STDMETHOD(OnInitIDA)() = 0;
// called at the end of each survey scan
STDMETHOD(OnScreenSurveySpec)() = 0;
// called at the time of switch
STDMETHOD(OnPrepareNextScan)(double* selectedMasses, double* selectedIntensities, long* selectedCharges, int itemCount) = 0;
In my new dll using ATL I tried to mimic this and the definitions are below: 在我使用ATL的新dll中,我试图模仿这一点,其定义如下:
STDMETHOD(GetSwitchCriteria)(DOUBLE* intensity, DOUBLE* minMass, DOUBLE* maxMass, VARIANT_BOOL* selectIntensity, LONG* numOfDepCycles);
STDMETHOD(GetChargeStateParam)(SHORT* minCharge, SHORT* maxCharge, VARIANT_BOOL* doChargeState);
STDMETHOD(GetInclusionList)(DOUBLE* intensity, DOUBLE* theList, SHORT* numOfItems);
STDMETHOD(GetExclusionList)(LONG* exRTWindow, DOUBLE* theMassList, LONG* theRTList, SHORT* numOfItems);
STDMETHOD(GetOtherCriteria)(LONG* smartFilterTime, DOUBLE* isoExclusionWin, DOUBLE* massTolerance, VARIANT_BOOL* isPPM);
STDMETHOD(GetIsotopeMatchParam)(DOUBLE* theMassList, DOUBLE* theAbundanceList, DOUBLE* abTolerance, DOUBLE* maTolerance);
STDMETHOD(OnInitIDA)(void);
STDMETHOD(OnScreenSurveySpec)(void);
STDMETHOD(OnPrepareNextScan)(DOUBLE* selectedMasses, DOUBLE* selectedIntensities, LONG* selectedCharges, LONG itemCount);
Originally I had used BOOL for some of the parameters but I noticed that the C# dll used VARIANT_BOOL when I looked in the TLB file for it. 最初,我使用BOOL作为某些参数,但是当我在TLB文件中查找C#dll时,我注意到它使用了VARIANT_BOOL。
When I call the two methods that have no parameters I get an HRESULT of 0. The same does for the OnPrepareNextScan method. 当我调用两个没有参数的方法时,我得到的HRESULT为0。OnPrepareNextScan方法也是如此。 However when I call the others I get an exception, a buffer overrun and I am not sure why this is the case. 但是,当我打电话给其他人时,我得到一个例外,缓冲区溢出,我不确定为什么会这样。 Does anyone have an idea why this is happening or how I can investigate this? 有谁知道为什么会这样或我如何调查呢?
Is this likely due to a type incompatibility between the types defined in both interfaces and possibly the types I use when creating the variables for use with the method? 这是否可能是由于两个接口中定义的类型之间的类型不兼容,也可能是我创建用于该方法的变量时使用的类型之间的不兼容? An example of the code used to call one of the methods is: 用于调用方法之一的代码示例为:
short minCharge = 0;
short maxCharge = 0;
BOOL doChargeState = FALSE;
result = m_pUserIDA->GetChargeStateParam(&minCharge, &maxCharge, &doChargeState);
std::cout << "HRESULT of GetChargeStateParam: " << result << std::endl;
Tried the LONGLONG solution mentioned below but it did not seem to help. 尝试了下面提到的LONGLONG解决方案,但似乎没有帮助。
I thought I should post the original interface from the C# dll. 我以为我应该从C#dll发布原始接口。
[Guid("76F452FF-7A89-11d4-8A2C-00B0D023C6A0")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public unsafe interface IUserIDA
{
[DispId(1)]
void GetSwitchCriteria(ref double intensity, ref double minMass, ref double maxMass, ref bool selectIntensity, ref long numOfDepCycles);
[DispId(2)]
void GetChargeStateParam(ref short minCharge, ref short maxCharge, ref bool doChargeState);
[DispId(3)]
void GetInclusionList(ref double intensity, ref double theList, ref short numOfItems);
[DispId(4)]
void GetExclusionList(ref long exRTWindow, ref double theMassList, ref long theRTList, ref short numOfItems);
[DispId(5)]
void GetOtherCriteria(ref long smartFilterTime, ref double isoExclusionWin, ref double massTolerance, ref bool isPPM);
[DispId(6)]
void GetIsotopeMatchParam(ref double theMassList, ref double theAbundanceList, ref short numOfItems, ref double abTolerance, ref double maTolerance);
[DispId(7)]
void OnInitIDA();
[DispId(8)]
void OnScreenSurveySpec();
[DispId(9)]
void OnPrepareNextScan(double* selectedMasses, double* SelectedIntensities, long* selectedCharges, int itemCount);
}
The last method, OnPrepareNextScan is the only one that seems to work, and you can see it is definted using normal pointers rather than .NET ref statements, could this be a problem? 最后一种方法OnPrepareNextScan是唯一可行的方法,您可以看到它是使用普通指针而不是.NET ref语句定义的,这可能是问题吗?
I thought I might also include the Type Lib files for each, perhaps a difference in those will make it clear what the problem is and any differences between the two: 我以为我可能还会为每个文件都包含Type Lib文件,也许它们之间的差异将使问题出在哪里,以及两者之间的任何区别:
The original C# dll: 原始的C#dll:
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: AnalystIDA.dll
[
uuid(7ED40FB0-D178-47FA-9EF3-B434E0803D5F),
version(1.0),
custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "Analyst_UserIDA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=de4cf4f15e545467")
]
library Analyst_UserIDA
{
// TLib : // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
importlib("mscorlib.tlb");
// TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
importlib("stdole2.tlb");
// Forward declare all types defined in this typelib
interface IUserIDA;
interface _Utility;
[
uuid(CDD947DD-7B9E-382E-9F6F-55218F927C3A),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Analyst_UserIDA.Utility")
]
coclass Utility {
[default] interface _Utility;
interface _Object;
};
[
odl,
uuid(76F452FF-7A89-11D4-8A2C-00B0D023C6A0),
version(1.0),
oleautomation,
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Analyst_UserIDA.IUserIDA")
]
interface IUserIDA : IUnknown {
HRESULT _stdcall GetSwitchCriteria(
[in, out] double* intensity,
[in, out] double* minMass,
[in, out] double* maxMass,
[in, out] VARIANT_BOOL* selectIntensity,
[in, out] int64* numOfDepCycles);
HRESULT _stdcall GetChargeStateParam(
[in, out] short* minCharge,
[in, out] short* maxCharge,
[in, out] VARIANT_BOOL* doChargeState);
HRESULT _stdcall GetInclusionList(
[in, out] double* intensity,
[in, out] double* theList,
[in, out] short* numOfItems);
HRESULT _stdcall GetExclusionList(
[in, out] int64* exRTWindow,
[in, out] double* theMassList,
[in, out] int64* theRTList,
[in, out] short* numOfItems);
HRESULT _stdcall GetOtherCriteria(
[in, out] int64* smartFilterTime,
[in, out] double* isoExclusionWin,
[in, out] double* massTolerance,
[in, out] VARIANT_BOOL* isPPM);
HRESULT _stdcall GetIsotopeMatchParam(
[in, out] double* theMassList,
[in, out] double* theAbundanceList,
[in, out] short* numOfItems,
[in, out] double* abTolerance,
[in, out] double* maTolerance);
HRESULT _stdcall OnInitIDA();
HRESULT _stdcall OnScreenSurveySpec();
HRESULT _stdcall OnPrepareNextScan(
[in, out] double* selectedMasses,
[in, out] double* SelectedIntensities,
[in, out] int64* selectedCharges,
[in] long itemCount);
};
[
uuid(5B2DBDD4-B763-428A-B48F-2E148138E7A4),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Analyst_UserIDA.UserIDAObject")
]
coclass UserIDAObject {
interface _Object;
[default] interface IUserIDA;
};
[
odl,
uuid(8094073C-362D-37BB-9791-E9D163271C73),
hidden,
dual,
oleautomation,
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Analyst_UserIDA.Utility")
]
interface _Utility : IDispatch {
};
};
My new dll: 我的新dll:
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: AnalystIDA.dll
[
uuid(7ED40FB0-D178-47FA-9EF3-B434E0803D5F),
version(1.0),
helpstring("AnalystIDA 1.0 Type Library"),
custom(DE77BA64-517C-11D1-A2DA-0000F8773CE9, 117441012),
custom(DE77BA63-517C-11D1-A2DA-0000F8773CE9, 1329510523),
custom(DE77BA65-517C-11D1-A2DA-0000F8773CE9, "Created by MIDL version 7.00.0500 at Fri Feb 17 15:28:38 2012
")
]
library AnalystIDALib
{
// TLib : // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
importlib("stdole2.tlb");
// Forward declare all types defined in this typelib
interface IUserIDA;
[
uuid(5B2DBDD4-B763-428A-B48F-2E148138E7A4),
helpstring("UserIDAObject Class")
]
coclass UserIDAObject {
[default] interface IUserIDA;
};
[
odl,
uuid(76F452FF-7A89-11D4-8A2C-00B0D023C6A0),
helpstring("IUserIDA Interface"),
dual,
nonextensible,
oleautomation
]
interface IUserIDA : IDispatch {
[id(0x00000001), helpstring("method GetSwitchCriteria")]
HRESULT GetSwitchCriteria(
[in, out] double* intensity,
[in, out] double* minMass,
[in, out] double* maxMass,
[in, out] VARIANT_BOOL* selectIntensity,
[in, out] long* numOfDepCycles);
[id(0x00000002), helpstring("method GetChargeStateParam")]
HRESULT GetChargeStateParam(
[in, out] short* minCharge,
[in, out] short* maxCharge,
[in, out] VARIANT_BOOL* doChargeState);
[id(0x00000003), helpstring("method GetInclusionList")]
HRESULT GetInclusionList(
[in, out] double* intensity,
[in, out] double* theList,
[in, out] short* numOfItems);
[id(0x00000004), helpstring("method GetExclusionList")]
HRESULT GetExclusionList(
[in, out] long* exRTWindow,
[in, out] double* theMassList,
[in, out] long* theRTList,
[in, out] short* numOfItems);
[id(0x00000005), helpstring("method GetOtherCriteria")]
HRESULT GetOtherCriteria(
[in, out] long* smartFilterTime,
[in, out] double* isoExclusionWin,
[in, out] double* massTolerance,
[in, out] VARIANT_BOOL* isPPM);
[id(0x00000006), helpstring("method GetIsotopeMatchParam")]
HRESULT GetIsotopeMatchParam(
[in, out] double* theMassList,
[in, out] double* theAbundanceList,
[in, out] double* abTolerance,
[in, out] double* maTolerance);
[id(0x00000007), helpstring("method OnInitIDA")]
HRESULT OnInitIDA();
[id(0x00000008), helpstring("method OnScreenSurveySpec")]
HRESULT OnScreenSurveySpec();
[id(0x00000009), helpstring("method OnPrepareNextScan")]
HRESULT OnPrepareNextScan(
[in, out] double* selectedMasses,
[in, out] double* selectedIntensities,
[in, out] long* selectedCharges,
[in] long itemCount);
};
};
Another Edit: I notice that the ProgID Analyst_UserIDA.UserIDAObject does not appear in my new dll's type lib. 另一个编辑:我注意到ProgID Analyst_UserIDA.UserIDAObject没有出现在我的新dll的类型lib中。 This is used in order to create an instance of the UserIDAObject in the test application: IUserIDAPtr m_pUserIDA; 这是为了在测试应用程序中创建UserIDAObject的实例:IUserIDAPtr m_pUserIDA; ::CoInitialize(m_pUserIDA); :: CoInitialize(m_pUserIDA); m_pUserIDA.CreateInstance(_T("Analyst_UserIDA.UserIDAObject")); m_pUserIDA.CreateInstance(_T(“ Analyst_UserIDA.UserIDAObject”));
More progress/confusion: I decided to see if there was a problem with the DLL by creating a C# project that would call this dll. 更多进步/困惑:我决定通过创建一个将调用此dll的C#项目来查看DLL是否存在问题。 When I did this the methods I have a problem with work. 当我这样做时,我的工作方式出现了问题。 I have also noticed that the methods that appeared to work originally do not. 我还注意到,最初看来可行的方法没有效果。 I added a sleep statement to one of them and it never appears to be executed when called by my test application. 我向其中之一添加了一个sleep语句,当我的测试应用程序调用该语句时,它似乎从未执行过。
Note that in Win32 a LONG
is a 32-bit integer, while in C# a long
is a 64-bit integer. 请注意,在Win32中, LONG
是32位整数,而在C#中, long
是64位整数。 That may cause the problems that you describe. 这可能会导致您描述的问题。
The solution is to use a __int64
or a LONGLONG
whenever C# uses a long
. 解决方案是,每当C#使用long
时,都使用__int64
或LONGLONG
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.