将句柄传递给控制台应用程序或托管/非托管帮助

发布于 2024-10-04 03:34:18 字数 1977 浏览 8 评论 0原文

我就是因为这个才出现睡眠问题的!我有一个 VS2005 C# DLL,它应该与 Labview 下的 USB 设备进行通信。 C# DLL 安装在 C++ 包装器上,而 C++ 包装器位于 C++(非托管)项目上(不是我编码的,但我拥有代码。顺便说一句。C++ 和我,我们不是最好的朋友)。

使用这个包装器,我可以(在 Windows/Visual Studio 下)执行所有操作(连接、断开连接、发送和接收数据)。 Labview下出现这个问题。它连接、断开连接、发送文件,但不接收(不是很有用)。我调试了代码,知道问题出在哪里,但不知道如何解决。 (我可以尝试解释一下)

因为我认为这是修复非托管库的更长方法,所以我意识到通过编写一个处理接收例程的控制台应用程序,我可以跳过这个问题。控制台应用程序作为进程从 C# DLL 调用。在此过程中,它断开与DLL的连接,调用再次连接的ConsoleApp,请求文件,将其保存到HD并断开连接。 C# Dll 重新连接并加载文件。

正如您所想,这需要一些很长/不切实际的时间才能完成。我考虑了两个选项/问题:

有没有一种方法可以将设备的打开引用传递给 ConsoleApp(Handle、Ptr 或类似的字符串参数),这样我就不必再次连接,而只需请求。如何 ?

或者修复非托管代码应该更容易,这样我就不会遇到此问题并且可以直接从 C# DLL 工作?

托管/非托管是这样的:

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);
};

问题是,当非托管代码调用 mInferface->传递(mMessage) ; mParent 没有实例。然后,在Wrapper中,mParent为空(value = null?);就像它只能访问非托管 IInterface 中的方法,而不是包装器 CInterface 中的 Wrapper^ 一样。当尝试评估 mParent->Parse 时会发生崩溃。 gcroot 抛出 GCHandle AppDomain 异常。 ?

我应该怎么办 ??

谢谢 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2024-10-11 03:34:18

老实说,使用控制台应用程序来完成与 USB 接口的所有操作,然后让 LabVIEW 通过 TCP 与应用程序进行通信会更容易(因为它是内置的,并且与某些其他方法)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文