将句柄传递到控制台应用程序或托管/非托管帮助 [英] Passing Handle to a console app OR Managed/Unmanaged Help

查看:198
本文介绍了将句柄传递到控制台应用程序或托管/非托管帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有睡眠问题,因为这个!我有一个VS2005 C#DLL,应该在Labview下与USB设备通信。 C#DLL被安装在一个C ++ Wrapper上,它是一个C ++(非管理)项目(不是由我编码,但我拥有代码。btw。C ++和我,我们不是最好的朋友)。

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

使用这个包装我可以(在Windows / Visual Studio下)做一切(连接,断开连接,发送和接收数据)。问题发生在Labview下。它连接,断开连接,发送文件,但它不接收(不是很有用)。我已经调试了代码,知道问题在哪里,但我不知道如何解决它。 (我可以TRY解释它)

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)

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

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:

有一种方法,我可以传递给控制台应用程序打开的设备引用(Handle,Ptr或类似字符串arg ),所以我不会再次连接,但只有请求。怎么样 ?

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 ?

或者它应该更容易修复非托管代码,所以我不会有这个问题,我可以直接从C#DLL工作

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

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

未管理:(commLayer.h)

Unmanaged:(commLayer.h)

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

未管理:(IInterface.h)

Unmanaged:(IInterface.h)

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

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

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

我该怎么办?

谢谢!

推荐答案

老实说,只需使用控制台应用程序做与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).

这篇关于将句柄传递到控制台应用程序或托管/非托管帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆