本文介绍了如何在C#代码中处理C ++ OLE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我严重陷入了一个用C#编写的应用程序中,该应用程序试图从C ++中生成的OLE发送和接收数据.基本上,最初,该应用程序是用C ++编写的,用于与设备进行通信并获取设备的数据(序列号,型号等),我将其转换为C#代码.但是,我用于通信的OLE仍然是C ++.我可以使用OLE,但无法通过OLE从设备获取数据.
以下为示例.

用C ++编写的代码


/*********************************************** ****************************************
OLESendCommand
用于发送和接收的功能来自设备的数据

Hi Everyone,

I stuck up badly in a application written in C# where I am trying to send and receive data from OLE which is build in C++. Basically originally this application was written in C++ about communication with device and getting device's data(Sr.no,model no etc.), which I converted in C# code. But still OLE I am using for communication is in C++. I am able to talk with OLE but unable to get data from device through OLE.
Following is an example.

Code written in C++


/*******************************************************************************
 OLESendCommand
  the function used to send and receive data from the device

公用成员变量'UsingTunnel'被传递给OLE函数.

  The public member variable 'UsingTunnel' is passed to the OLE function.


subsys:子系统
cmd:命令
reg8:作为第四个传输字节的选项字节
dataout:32位,低字节序数据.
这形成了有效负载字节4,5,6,7(0、1、2、3、4、5、6、7字节之外)


    subsys : the subsystem
    cmd : the command
    reg8 : an option byte that is the fourth transmitted byte
    dataout : 32 bit, little endian data.
     this forms the payload bytes 4,5,6,7 (out of 0,1,2,3,4,5,6,7)

*结果信息:这是从事务返回的结果
通常是RET_ACK或RET_DATA32或RET_NAK.
* recvbuffer:必须提供8个字节长的uchar缓冲区. ,仅填充前四个字节,对应于(0,1,2,3,4,5,6,7)中的字节
4,5,6,7

    *resultinformation : this is the result returned from the transaction
     often will be RET_ACK or RET_DATA32 or RET_NAK.
       
    *recvbuffer : must be supplied with a uchar buffer 8 bytes long.
     generally, only the first four bytes are filled, corresponding to the bytes
        4,5,6,7 out of (0,1,2,3,4,5,6,7)

长格式命令最多可返回8个字节

        up to 8 bytes can be returned for long format commands

如果设备没有答复,则返回非零值.

    A non zero value is returned if there was no reply from the device.

*************************************************** ************************************/
uint32 CCOMLib2 :: OLESendCommand( uint32子系统,
uint32 cmd,
uint32 reg8,
uint32数据输出,
uint32 * resultinformation,
uint8 * recvbuffer//buffer 8 deep
){
uint32 result;
try {结果,导致= V.OleFunction(QUOT; SendCommandTemp",UsingTunnel,结果,DeviceAddress,结果,SUBSYS,结果,CMD,结果,REG8,结果,DATAOUT,结果,resultinformation,结果,( uint32 *)& recvbuffer [0],
(uint32 *)& recvbuffer [4]);
}
catch(Exception& E){

*******************************************************************************/
uint32 CCOMLib2::OLESendCommand(
     uint32 subsys,
    uint32 cmd,
    uint32 reg8,
    uint32 dataout,
    uint32 * resultinformation,
    uint8 * recvbuffer //buffer 8 deep
) {
 uint32 result;
   try  {
  result=V.OleFunction("SendCommandTemp",UsingTunnel,
            DeviceAddress,
            subsys,
            cmd,
            reg8,
            dataout,
            resultinformation,
            (uint32*) &recvbuffer[0],
            (uint32*) &recvbuffer[4]);
      }
   catch (Exception &E)   {

LM(``无法使用``+
AnsiString(E.Message),3)访问ole函数;
返回2;

       LM("Unable to access ole function with "+
       AnsiString(E.Message),3);
        return 2;

}
如果(结果)LM(失败.");

      }
   if (result) LM("failed.");

返回结果;
}

   return result;
}

公共 不安全 Int32 OLESendCommand( Int32 subsys, Int32 cmd, Int32 reg8, Int32 数据输出,

public unsafe Int32 OLESendCommand(Int32 subsys, Int32 cmd, Int32 reg8, Int32 dataout,

Int32 * resultinformation信息, ref byte [] recvbuffer) //buffer 8深

Int32* resultinformation,ref byte[] recvbuffer) //buffer 8 deep

{

Int32 结果;

Int32 result;

尝试

try

{

//result = V.OleFunction("SendCommandTemp",UsingTunnel,

// result = V.OleFunction("SendCommandTemp", UsingTunnel,

//DeviceAddress,subsys,cmd,reg8,dataout,resultinformation,

// DeviceAddress, subsys, cmd, reg8, dataout, resultinformation,

//ref recvbuffer [0],ref recvbuffer [4]);

// ref recvbuffer[0], ref recvbuffer[4]);

result =( Int32 )V.GetType().InvokeMember( "SendCommandTemp" ,System.Reflection. BindingFlags .InvokeMethod, null ,V,

result = (Int32)V.GetType().InvokeMember("SendCommandTemp", System.Reflection.BindingFlags.InvokeMethod, null, V,

对象 [] { usingTunnel,DeviceAddress,subsys,cmd,reg8,dataout,( Int32 )结果信息,

new object[] { UsingTunnel, DeviceAddress, subsys, cmd, reg8, dataout,(Int32)resultinformation,

recvbuffer [0],recvbuffer [4]});

recvbuffer[0], recvbuffer[4] });

}

捕获 ( 例外 e)

catch (Exception e)

{

LM( "无法使用 + e.Message,3)访问ole函数;

LM("Unable to access ole function with "+e.Message,3);

返回 2;

return 2;

}

if (结果!= 0)LM( 失败." );

if (result != 0) LM("failed.");

返回 结果;

return result;

}

推荐答案

感谢您的帖子!我建议将您的问题发布到其中一个

Thank you for your post!  I would suggest posting your question in one of the 

MSDN > 论坛首页> Visual C#> Visual C#General
MSDN > Forums Home > Visual C# > Visual C# General


这篇关于如何在C#代码中处理C ++ OLE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 00:29