0

I am working on a project that involves the creation of a dll that honours a certain interface in order to plug into some software in order to add functionality to it. This is done by a dll that calls my dll (I do not have the source code for the dll that does the calling). Originally I was given an interface and a C# implementation that created a COM visible dll. However after using this for a while I found I wanted to make use of some large C++ libraries and as creating wrappers would take a long time I thought about creating a C++ ATL COM dll instead. I did this and the methods of my class appear to be called correctly (I register my dll, run the program and the methods appear to be called in the correct order), however I have found some of the behavior to be different.

I am not sure how to go about explaining this as my code relates to a closed source API but perhaps if I describe an example someone might have some ideas as to where I might want look.

For instance, in the C# dll I attempt to open a file by doing this:

FMANFileControl fileControl = new FMANFileControl();
FMANFile wFile = null;
const string filePath = @"C:\Data\April 4\Data_IDA.wiff";
wFile = fileControl.GetFileObject(filePath, 1);
long numSamples = wFile.GetNumberOfSamples();

I get the correct number of samples.

In my C++ dll I have this (with some of the HRESULT checks removed in order to keep the code shorter):

std::string filePath = "C:\\Data\\April 4\\Data_IDA.wiff";
_bstr_t fileName(filePath.c_str());
IFMANFilePtr ipFMANFile;
IFMANFileControlPtr ipFMANFileControl;
hr = ipFMANFileControl.CreateInstance(__uuidof(FMANFileControl));
hr = ipFMANFile.CreateInstance(__uuidof(FMANFile));
ipFMANFile = ipFMANFileControl->GetFileObject(fileName, 1);
long numSamples = ipFMANFile->GetNumberOfSamples(); 

but the files does not open correctly, resulting is zero samples.

Using oleview I looked at the typelib and it says this for the function:

[id(0x00000001), helpstring("method GetWiffFileObject")] 
IFMANWiffFile* GetWiffFileObject( [in] BSTR WiffFileName, [in] long sample); 

The file I get information from is one that is being written to during an experiment and just before it obtains more data it calls my method and I should be able to obtain the newest file. In the C# dll this is possible, but in the C++ dll this is not. While I realize the specifics of this is hidden, I am wondering is anyone has any idea why a C++ COM dll and a C#, comvisible dll that make use of the same interface would exhibit different behavour when being called by the same dll.

I am pretty stumped at this moment so any ideas at all would be appreciated, even if they turn out to be way off base. I can share my source code if anyone thinks they might be able to help.

EDIT: I tried the solution to answer 1, however I could not compile my code. When reading about this I found this post: Differences between [in, out] and [out, retval] in COM IDL definitions that seems to suggest that since the FMANFile pointer is marked [out, retval] that the method becomes:

IFMANFilePtr ExploreData::IFMANFileControl(BSTR filename, long sample);

or am I misinterpreting that article?

EDIT 2: Got it working though I am not really sure why. Originally I had the variables declared in the header as private member variables of the class, like this:

class ATL_NO_VTABLE CUserIDA :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CUserIDA, &CLSID_UserIDAObject>,
public IUserIDA
{
.
.
.
public:
STDMETHOD(GetSwitchCriteria)(DOUBLE* intensity, DOUBLE* minMass, DOUBLE* maxMass, VARIANT_BOOL *selectIntensity, LONG* numOfDepCycles);
    .
    .
    .
private:
    ExploreDataObjects::IFMANWiffFilePtr ipFMANWiffFile;
ExploreDataObjects::IFMANWiffFile2Ptr ipFMANWiffFile2;
};

Just to try it I moved them to the top of the class delcaration like this:

class ATL_NO_VTABLE CUserIDA :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CUserIDA, &CLSID_UserIDAObject>,
public IUserIDA
{

ExploreDataObjects::IFMANWiffFilePtr ipFMANWiffFile;
ExploreDataObjects::IFMANWiffFile2Ptr ipFMANWiffFile2;

I thought that by default these would also be private members and the same as before so I am at a loss to explain why this seemed to work. Can someone explain this?

4

1 回答 1

0

您的 C++ 代码是正确的,但以下行除外:

hr = ipFMANFile.CreateInstance(__uuidof(FMANFile));

它没有任何意义,因为 ipFMANFile 在下一条语句中再次初始化。

不幸的是,这个 IDL 声明:

IFMANWiffFile* GetWiffFileObject([in] BSTR WiffFileName, [in] long sample); 

仅限于调试目的,因为它不支持通过 HRESULT 报告异常的本机 COM 机制。符合 COM 的声明将是:

HRESULT GetWiffFileObject([in] BSTR WiffFileName, [in] long sample, [out, retval] IFMANWiffFile** fileInstance); 

我相信您无法更改库的代码,因此我建议您在运行 CPP 测试用例时尝试一些外部调试工具,如“procmon.exe”和“dbgview.exe”来检查应用程序事件。查找所有失败的操作。

我希望这会以某种方式帮助你

于 2012-04-10T07:04:59.617 回答