Passing Handle to a console app OR Managed/Unmanaged Help

巧了我就是萌 提交于 2019-12-11 06:36:16

问题


I'm having sleeping issues because of this! I have a VS2005 C# DLL that should communicate to a USB device under Labview. The C# DLL is mounted over a C++ Wrapper who's over a C++ (unmanaged) Project (not coded by me, but I own the code. btw. C++ and me, we're not best friends).

Using this wrapper I can (under Windows/Visual Studio) do everything (connect, disconnect, send and receive data). The problem occurs under Labview. It connects, disconnects, send files but it doesn't receives(not very useful). I've debugged the code, know where the problem is, but i don't know how to fix it. (I could TRY to explain it)

Since i thought it was a longer way to fix the unmanaged library, I realized that by coding a Console App that handled the Receive routine, i could jump over this issue. The Console App is called from the C# DLL as a process. In this process, it disconnects from the DLL, calls the ConsoleApp who connects once again, requests the file, saves it to the HD and disconnects. The C# Dll reconnects and loads the file.

As you could think, this takes some long/unpractical time to complete. I thought about two options/questions:

Is there a way that I could pass to the ConsoleApp the opened reference of the Device(Handle, Ptr or similiar as a string arg), so that i wouldn't have to connect again but only request. How ?

OR it should be easier to fix the unmanaged code so that i won't have this issue and i could work directly from the C# DLL ?

The Managed/Unmanaged goes something like this:

Wrapper:(wrapper.h)

public ref class Wrapper
{
public:
   Send(String^ mSendMessage);
   Parse(String^ mMessageString);
...
private:
   ComLayer* mComm;
   CInferface mInterface;
};

private class CInterface : public IIterface
{
public:
   virtual bool Deliver(CMessage mMessage);
...
private:
   gcroot<Wrapper^> mParent;
};

Wrapper(wrapper.cpp)

Wrapper::Send(String^ mSendMessage)
{
...
mComm->Send(mMessage);
}
Wrapper::Parse(String^ mMessageString)
{
...
}

CInterface::Deliver(CMessage* mMessage)
{
...
//Here, mParent value is empty under Labview, not while Debug/VS/WindowsForm
mParent->Parse(mMessageString)
}

Unmanaged:(commLayer.h)

class CommLayer
{
public:
//Send:
   bool Send(CMessage* mMessage);
...
private:
//instead of CInterface, IInterface.
   IInterface mInterface;
};

Unmanaged:(IInterface.h)

class IInterface
{
public:
//Response:
   virtual bool Deliver(CMessage mMessage);
};

The problem is that when the unmanaged code calls the mInferface->Deliver(mMessage) ; There is no instance for mParent. Then, in the Wrapper, mParent is empty (value = null ?); Is like it would only access the methods from the Unmanaged IInterface and not the Wrapper^ from the wrapper CInterface. The crash comes when trying to evaluate mParent->Parse. The gcroot throws an GCHandle AppDomain Exception. ?

What should I do ??

Thanks !


回答1:


Honestly, it'd be easier to just use your console app to do everything in terms of interfacing with the USB, then just have LabVIEW communicate with the app via TCP (just because it's built in and relatively easy to use compared to some of the other methods).



来源:https://stackoverflow.com/questions/4266427/passing-handle-to-a-console-app-or-managed-unmanaged-help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!