typedef struct tagCMDRDWRBYPIPE
{
CMDPIPE           CmdPipe;
union{
   DBREQUESTFORREAD   RequestForRead;
   DBREQUESTFORWRITE  RequestForWrite;
   REQUESTFOROPINFO   RequestForOpInfo; };
}CMDRDWRBYPIPEtypedef struct tagCmdPipe
{
char byReserved;
}CMDPIPE
typedef struct tagDBRquestForRead
{
char  chrStationName[61];
char  chrTableName[61];
char  chrRecorderName[61];
char  chrFieldName[61];
}DBREQUESTFORREADBOOL CPipe::ReadDataBase(const CMDRDWRBYPIPE &request)
{    
char buf[2000];
*((CMDPIPE*)buf) = request.CmdPipe;
*((DBREQUESTFORREAD*)(buf + sizeof(CMDPIPE))) = request.RequestForRead;
if(WritePipe((LPCVOID)buf, sizeof(CMDRDWRBYPIPE))){
   return ReadPipe();
}
return FALSE;
}上面的代码用vb应该怎么写呀?谢谢~

解决方案 »

  1.   

    谢谢~~xanger(do while MYM<400000)百忙之中还帮我顶!!!
    感动!!!
      

  2.   

    大致是这个样子:
    Type DBREQUESTFORREAD
        chrStationName As String * 61
        chrTableName As String * 61
        chrRecorderName As String * 61
        chrFieldName As String * 61
    End TypeType DBREQUESTFORWRITE
    '......
    End TypeType REQUESTFOROPINFO
    '......
    End TypeType CMDPIPE
        byReserved As Byte
    End TypeType CMDRDWRBYPIPE
    myCmdPipe As CMDPIPE
    RequestForRead As DBREQUESTFORREAD
             RequestForWrite As DBREQUESTFORWRITE
    myRequestForOpInfo As REQUESTFOROPINFO
    End TypePublic Function ReadDataBase(request As CMDRDWRBYPIPE) As Boolean
    Dim buf As(2000) As byte buf(0) = request.CmdPipe
    buf(sizeof(CMDPIPE)) = request.RequestForRead;
    if (WritePipe(buf, sizeof(CMDRDWRBYPIPE))) Then
       ReadDataBase = ReadPipe()
                Exit Function
    End If
    ReadDataBase = FALSEEnd Function
      

  3.   

    of123() 您费心了!不过我觉得好像不太对!
    *((DBREQUESTFORREAD*)(buf + sizeof(CMDPIPE))) = request.RequestForRead;
    是把指针移位再传内容!您这个buf(sizeof(CMDPIPE)) = request.RequestForRead;是什么意思我没看懂!不好意思!
      

  4.   

    1 vb中没有sizeof,可以考虑用len替换它
    2 把指针移位再传内容可以考虑使用copymemory
      

  5.   

    由于VB的String是基于Unicode的,会存在自动转换
    为了安全性,char绝对不能定义为String*n,只能定义为Byte数组
      

  6.   

    //为了安全性,char绝对不能定义为String*n,只能定义为Byte数组严重同意,不过这样的话,赋值就要麻烦一些了,可以考虑用copymemory解决
      

  7.   

    Type tagCMDPIPE '处理同名问题
        byReserved(0 to 61-1) As byte
    End TypeType DBREQUESTFORREAD
        chrStationName(0 to 61-1) As byte
        chrTableName(0 to 61-1) As byte
        chrRecorderName(0 to 61-1) as byte
        chrFieldName(0 to 61-1) As byte
    End Typetype CMDRDWRBYPIPE
        CmdPipe as tagCMDPIPE
        RequestForRead as DBREQUESTFORREAD 'VB不支持联合体,实际上这里应该写最大的那个结构体
    end typePublic Function ReadDataBase(byref request As CMDRDWRBYPIPE) As Boolean
            Dim buf As(0 to 2000-1) As byte CopyMemory byval CLng(varptr(buf(0))+0), request.CmdPipe, lenb(request.CmdPipe)
    CopyMemory byval CLng(varptr(buf(0))+lenb(request.CmdPipe)), request.RequestForRead, lenb(request.RequestForRead)
    if (WritePipe(buf, lenb(request.CmdPipe)+lenb(request.RequestForRead))) Then
        ReadDataBase = ReadPipe()
                Exit Function
    End If
    ReadDataBase = FALSEEnd Function
      

  8.   

    Type tagCMDPIPE '处理同名问题
        byReserved As byte
    End Type
      

  9.   

    CopyMemory的声明是这样吗?
    Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
    那这句:CopyMemory byval CLng(varptr(buf(0))+0), request.CmdPipe, lenb(request.CmdPipe)
    第二个参数类型不匹配吧!请再指点!谢谢
      

  10.   

    Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (byref hpvDest As Any, Byref hpvSource As any, ByVal cbCopy As Long)
      

  11.   

    #define MAX_NAME_BYTE 61
    typedef struct tagCmdPipe
    { BYTE byCmdCode;                      
    BYTE byWrite;                        
    BYTE byShare;                        
    char chrNameSharedMap[MAX_NAME_BYTE]; 
    char byReserved;
    }CMDPIPE, *LPCMDPIPE;typedef struct tagDBRquestForRead
    {
    char  chrStationName[MAX_NAME_BYTE];
    char  chrTableName[MAX_NAME_BYTE];
    char  chrRecorderName[MAX_NAME_BYTE];
    char  chrFieldName[MAX_NAME_BYTE]; char  chrReserved1[MAX_NAME_BYTE];
    char  chrReserved2[MAX_NAME_BYTE];
    char  chrReserved3[MAX_NAME_BYTE];
    char  chrReserved4[MAX_NAME_BYTE];}DBREQUESTFORREAD, *LPDBREQUESTFORREAD;
    typedef struct tagRequestForWrite
    {
    char  chrStationName[MAX_NAME_BYTE];
    char  chrTableName[MAX_NAME_BYTE];
    char  chrRecorderName[MAX_NAME_BYTE];
    char  chrFieldName[MAX_NAME_BYTE]; union{
    BYTE    bData;
    WORD    wData;
    DWORD   dwData;
    double  fData;
    short   nData;
    char    chData[MAX_NAME_BYTE];
    };
    }DBREQUESTFORWRITE, *LPDBREQUESTFORWRITE;
    typedef struct tagRequestForOpInfo
    {
    char  chrStationName[MAX_NAME_BYTE];
    char  chrTableName[MAX_NAME_BYTE];
    char  chrRecorderName[MAX_NAME_BYTE]; char  chrOperator[MAX_NAME_BYTE];
    BYTE  byOpGoal;//breaker:    
    //transformer 10--prompt;  
    BYTE       byHVORMV;
    SYSTEMTIME OpTime; //
    SYSTEMTIME CompleteTime;//
    BYTE byResult;/
    }REQUESTFOROPINFO, * LPREQUESTFOROPINFO;typedef struct tagCMDRDWRBYPIPE
    {
    CMDPIPE           CmdPipe;
    union{
       DBREQUESTFORREAD   RequestForRead;
       DBREQUESTFORWRITE  RequestForWrite;
       REQUESTFOROPINFO   RequestForOpInfo; };
    }CMDRDWRBYPIPE, *LPCMDRDWRBYPIPE;BOOL CPipe::ReadDataBase(const CMDRDWRBYPIPE &request)
    {
    ASSERT(m_hPipe!=NULL);    
    char buf[PIPE_WRITE_BUF_SIZE];
    *((CMDPIPE*)buf) = request.CmdPipe;
    *((DBREQUESTFORREAD*)(buf + sizeof(CMDPIPE))) = request.RequestForRead;
    if(WritePipe((LPCVOID)buf, sizeof(CMDRDWRBYPIPE))){
       return ReadPipe();
    }
    return FALSE;
    }
    BOOL WritePipe(LPCVOID lpVoid, int nBytesToWrite);
    BOOL ReadPipe(DWORD dwMilliseconds=10000);完整代码!谢谢!
      

  12.   

    Public Const MAX_NAME_BYTE = 61Public Type DBREQUESTFORREAD    chrStationName As String * MAX_NAME_BYTE
        chrTableName As String * MAX_NAME_BYTE
        chrRecorderName As String * MAX_NAME_BYTE
        chrFieldName As String * MAX_NAME_BYTE    chrReserved1 As String * MAX_NAME_BYTE
        chrReserved2 As String * MAX_NAME_BYTE
        chrReserved3 As String * MAX_NAME_BYTE
        chrReserved4 As String * MAX_NAME_BYTEEnd TypePublic Type DBREQUESTFORWRITE
        chrStationName As String * MAX_NAME_BYTE
        chrTableName As String * MAX_NAME_BYTE
        chrRecorderName As String * MAX_NAME_BYTE
        chrFieldName As String * MAX_NAME_BYTE    bData As Byte
        wData As Long
        dwData As Long
        fData As Double
        nData As Integer
        chData As String * MAX_NAME_BYTE
        
    End TypePublic Type RequestForOpInfo
        chrStationName As String * MAX_NAME_BYTE
        chrTableName As String * MAX_NAME_BYTE
        chrRecorderName As String * MAX_NAME_BYTE    chrOperator As String * MAX_NAME_BYTE
        byOpGoal As Byte 'breaker:     0--close;   1--open
        'transformer 10--prompt;  11--decrease;  12--stop
        byHVORMV As Byte  '0--HV;    1--MV;
        OpTime As SYSTEMTIME '执行时间
        CompleteTime As SYSTEMTIME '完成时间
        byResult As Byte '0-fail;1-success操作结果
    End TypePublic Type tagCmdPipe
        '0--单个查询; 1--批量查询;2--记录查询; 3---表查询;
        '4--单个测控点查询(只读不写)
        '5--报警查询;
        '6--24小报警查询;
        '操作员库查询--13(by pipe);    '20--write operate info
        byCmdCode As Byte
        byWrite As Byte                      '0--read;      1--write
        byShare As Byte                      '0---by pipe;  1--by shared map;
        chrNameSharedMap As String * MAX_NAME_BYTE '共享内存名
        byReserved As Integer
        
    End TypePublic Type CMDRDWRBYPIPE
        CmdPipe As tagCmdPipe
        
        RequestForRead As DBREQUESTFORREAD
    '    RequestForWrite As DBREQUESTFORWRITE
    '    RequestForOpInfo As RequestForOpInfo
        
    End TypePublic Function ReadDataBase(pos As Long) As Boolean
    Dim intLen As Integer
    Dim lp As Long
    Dim buf(2000) As ByteReadDataBase = False
    CopyMemory ByVal CLng(VarPtr(buf(0)) + 0), VarPtr(typeCMDRDWRBYPIPE(pos).CmdPipe), Len(typeCMDRDWRBYPIPE(pos).CmdPipe)CopyMemory ByVal CLng(VarPtr(buf(0)) + LenB(typeCMDRDWRBYPIPE(pos).CmdPipe)), VarPtr(typeCMDRDWRBYPIPE(pos).RequestForRead), Len(typeCMDRDWRBYPIPE(pos).RequestForRead)If WritePipe(buf, Len(typeCMDRDWRBYPIPE(pos).CmdPipe + LenB(typeCMDRDWRBYPIPE(pos).CmdPipe))) = True Then    ReadDataBase = ReadPipe()End IfEnd Function
      

  13.   

    copymemory时就报错,报类型不批配!
      

  14.   

    Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (byref hpvDest As Any, Byref hpvSource As any, ByVal cbCopy As Long)CopyMemory ByVal CLng(VarPtr(buf(0)) + 0), typeCMDRDWRBYPIPE(pos).CmdPipe, Lenb(typeCMDRDWRBYPIPE(pos).CmdPipe)CopyMemory ByVal CLng(VarPtr(buf(0)) + LenB(typeCMDRDWRBYPIPE(pos).CmdPipe)), typeCMDRDWRBYPIPE(pos).RequestForRead, Lenb(typeCMDRDWRBYPIPE(pos).RequestForRead)
      

  15.   

    你自己分析阿
    看问题是处在那个语句上(len?)注意Ctrl+F5是全编译运行,能检测语法错误
      

  16.   

    看在楼主给我发邮件的份上,我测试了一下该程序看来楼主真的不会发代码
    发来的代码不完整,不能调试
    还得我自己添变量、函数将函数添好后,按Ctrl+F5运行
    发现停在“If WritePipe(buf, Len(typeCMDRDWRBYPIPE(pos).CmdPipe + LenB(typeCMDRDWRBYPIPE(pos).CmdPipe))) = True Then”着一行,典型是括号没匹配
    看来楼主的调试水平不行啊Option Explicit
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Public Const MAX_NAME_BYTE = 61Public Type DBREQUESTFORREAD    chrStationName As String * MAX_NAME_BYTE
        chrTableName As String * MAX_NAME_BYTE
        chrRecorderName As String * MAX_NAME_BYTE
        chrFieldName As String * MAX_NAME_BYTE    chrReserved1 As String * MAX_NAME_BYTE
        chrReserved2 As String * MAX_NAME_BYTE
        chrReserved3 As String * MAX_NAME_BYTE
        chrReserved4 As String * MAX_NAME_BYTEEnd TypePublic Type DBREQUESTFORWRITE
        chrStationName As String * MAX_NAME_BYTE
        chrTableName As String * MAX_NAME_BYTE
        chrRecorderName As String * MAX_NAME_BYTE
        chrFieldName As String * MAX_NAME_BYTE    bData As Byte
        wData As Long
        dwData As Long
        fData As Double
        nData As Integer
        chData As String * MAX_NAME_BYTE
        
    End TypePublic Type RequestForOpInfo
        chrStationName As String * MAX_NAME_BYTE
        chrTableName As String * MAX_NAME_BYTE
        chrRecorderName As String * MAX_NAME_BYTE    chrOperator As String * MAX_NAME_BYTE
        byOpGoal As Byte 'breaker:     0--close;   1--open
        'transformer 10--prompt;  11--decrease;  12--stop
        byHVORMV As Byte  '0--HV;    1--MV;
        'OpTime As SYSTEMTIME '执行时间
        'CompleteTime As SYSTEMTIME '完成时间
        byResult As Byte '0-fail;1-success操作结果
    End TypePublic Type tagCmdPipe
        '0--单个查询; 1--批量查询;2--记录查询; 3---表查询;
        '4--单个测控点查询(只读不写)
        '5--报警查询;
        '6--24小报警查询;
        '操作员库查询--13(by pipe);    '20--write operate info
        byCmdCode As Byte
        byWrite As Byte                      '0--read;      1--write
        byShare As Byte                      '0---by pipe;  1--by shared map;
        chrNameSharedMap As String * MAX_NAME_BYTE '共享内存名
        byReserved As Integer
        
    End TypePublic Type CMDRDWRBYPIPE
        CmdPipe As tagCmdPipe
        
        RequestForRead As DBREQUESTFORREAD
    '    RequestForWrite As DBREQUESTFORWRITE
    '    RequestForOpInfo As RequestForOpInfo
        
    End TypePrivate typeCMDRDWRBYPIPE() As CMDRDWRBYPIPEPublic Function ReadDataBase(pos As Long) As Boolean
    Dim intLen As Integer
    Dim lp As Long
    Dim buf(2000) As ByteReadDataBase = False
    CopyMemory ByVal CLng(VarPtr(buf(0)) + 0), VarPtr(typeCMDRDWRBYPIPE(pos).CmdPipe), LenB(typeCMDRDWRBYPIPE(pos).CmdPipe)CopyMemory ByVal CLng(VarPtr(buf(0)) + LenB(typeCMDRDWRBYPIPE(pos).CmdPipe)), VarPtr(typeCMDRDWRBYPIPE(pos).RequestForRead), LenB(typeCMDRDWRBYPIPE(pos).RequestForRead)If WritePipe(buf, LenB(typeCMDRDWRBYPIPE(pos).CmdPipe) + LenB(typeCMDRDWRBYPIPE(pos).CmdPipe)) Then    ReadDataBase = ReadPipe()End IfEnd FunctionPublic Function WritePipe(ByRef buf() As Byte, ByVal nSize As Long) As Boolean
        
    End FunctionPublic Function ReadPipe() As Long
        
    End FunctionPrivate Sub Main()
        '
    End Sub
      

  17.   

    你的代码存在的问题
    1.Boolean比较问题
    VB的Boolean实际上是Integer,True = -1
    一般规定是非0为真
    判断Boolean为真应该是:If fbool then
    判断Boolean为假应该是:If fbool=False then2.String自动转换问题
    上面已经说过了3.结构体元素对齐
    n字节的类型必须排在n字节的地址
    比如你的“DBREQUESTFORWRITE”没有对其Public Type DBREQUESTFORWRITE
        chrStationName As String * MAX_NAME_BYTE
        chrTableName As String * MAX_NAME_BYTE
        chrRecorderName As String * MAX_NAME_BYTE
        chrFieldName As String * MAX_NAME_BYTE    bData As Byte '地址:61*4=244
        wData As Long '对其问题,VB编译器将空出3个字节,地址:61*4+4=248
        dwData As Long
        fData As Double
        nData As Integer
        chData As String * MAX_NAME_BYTE
        
    End Type
      

  18.   

    谢谢您这么耐心的讲解!
    再问一下高人,您讲的结构体问题该如何解决呢?
    我把代码补齐,您再看看!谢谢!Private Function WritePipe(buf() As Byte, nBytesToWrite As Integer) As Boolean
    Dim hEvent As Long
    Dim OverLap As OVERLAPPED
    Dim dwBytes As Long
    Dim lSuccess As Long
    Dim SA As SECURITY_ATTRIBUTEShEvent = CreateEvent(SA, True, False, vbNullString)OverLap.hEvent = hEvent
    OverLap.Offset = 0
    OverLap.OffsetHigh = 0lSuccess = WriteFile(m_hPipe, VarPtr(buf(0)), nBytesToWrite, dwBytes, OverLap)If lSuccess = 0 Then
        MsgBox "与实时库连接失败,请稍候再试!", vbExclamation, App.Title
        CloseHandle hEvent
        WritePipe = False
            
    Else
        CloseHandle hEvent
        WritePipe = True
        
    End IfEnd FunctionPrivate Function ReadPipe() As Boolean
    Dim hEvent As Long
    Dim OverLap As OVERLAPPED
    Dim dwBytes As Long
    Dim lSuccess As Long
    Dim SA As SECURITY_ATTRIBUTES
    Dim m_pBufToRcvMsghEvent = CreateEvent(SA, True, False, vbNullString)OverLap.hEvent = hEvent
    OverLap.Offset = 0
    OverLap.OffsetHigh = 0lSuccess = ReadFile(m_hPipe, m_pBufToRcvMsg, PIPEBUFSIZE, dwBytes, OverLap)If lSuccess = 0 Then
    '    MsgBox "实时库数据读取失败,请稍候再试!", vbExclamation, App.Title
        CloseHandle hEvent
        ReadPipe = False
            
    Else
        CloseHandle hEvent
        ReadPipe = True
        
    End If
    End Function
      

  19.   

    现在读写文件都失败了!
    我主要是完成读管道的功能,烦劳zyl910大哥再给看看,谢谢!
    m_hPipe是管道的句柄!
      

  20.   

    zyl910大哥,您去基础类里面找我的帖,我好给您分儿!谢谢!
      

  21.   

    是这个吧
    http://community.csdn.net/Expert/topic/3301/3301312.xml?temp=.8738672你为什么不把URL贴出来呢
      

  22.   

    烦劳zyl910大哥再给看看管道读写的问题!谢谢!
      

  23.   

    在论坛上问问题只是解决技术难题,不是抄代码!
    -- zyl910公告(2004/8/28) ---------昨天突然打雷下雨
    导致计算机突然断电
    我那时正在上网今天雨才停
    拨号一看
    “没有拨号音”我在家里翻箱倒柜
    总算找到一个Modem了
    可惜是14.4kbps的
    上网速度很不爽所以这几天可能不会上网了
    (再过几天就开学了)