我需用VC6做NetBios通信程序,请问:  
================以下是在C++  Builder  6中能正确执行的代码==============  
bool  CNetBios::Send(char  *callname,char  *SendData,int  len)  
{  
               bool  result=false;  
 
               if(ncbnum)  
               {  
                 memset((char*)&ncb,0,sizeof(NCB));  
                 ncb.ncb_command=NCBDGSEND;              //  
               ncb.ncb_num=ncbnum;  
                 ncb.ncb_buffer=SendData;  
                 ncb.ncb_length=len;  
                 strcpy(ncb.ncb_callname,callname);  
                 ncb.ncb_lana_num=uselana;  
                   if(Netbios(&ncb)==NRC_GOODRET)  
               {  
                 result=true;  
               }  
             }  
 
               
             return  result;  
}  
=============我在VC中写的代码======================  
bool  CNetBios::Send(CString  callname,CString  *SendData,int  len)  
{  
   bool  result=false;  
   if(ncbnum)  
   {  
         ZeroMemory(&ncb,sizeof(NCB));  
               //memset((char*)&ncb,0,sizeof(NCB));  
         ncb.ncb_command=NCBDGSEND;              //  
         ncb.ncb_num=ncbnum;  
         ncb.ncb_buffer=(unsigned  char  *)SendData;  
         ncb.ncb_length=len;  
               
         memcpy(ncb.ncb_callname,callname,strlen(callname));  
         ncb.ncb_lana_num=uselana;  
         if(Netbios(&ncb)==NRC_GOODRET)  
         {  
                 result=true;  
         }  
     }  
     return  result;  
}  
请问函数参数类型不同会影响通信吗?如果不会,那么我在执行以下回调函数是怎么就执行不完整,只是执行到void    CCNetBiosDlg::RecPostProgram(NCB  *ncb)的MessageBox(str),后面程序就没有反应了!究竟是那里出错了,程序编译都可以就是得不到正确结果!
void  __stdcall post(NCB * pn)  //POST例程
{
 CNetBiosDlg->RecPostProgram(pn);  
}void  CCNetBiosDlg::RecPostProgram(NCB *ncb)
{
  struct ZFS *recdata;
  CString str;
  //str.Format("%d",recdata);
  //MessageBox(str);
  if(ncb->ncb_length==sizeof(ZFS))
    {
     recdata=(struct ZFS *)ncb->ncb_buffer; 
     str.Format("%s",*recdata);
 MessageBox(str);
     Buffer[p_Buffer_In]=*recdata;    
     p_Buffer_In++;                      
     if(p_Buffer_In==50)     
        p_Buffer_In=0;
     }
   
  MessageBox("stonetu");
  PostMessage(WM_MESSAGE,0,0);
  MessageBox("stone");
  RecBios.Receive(post);         }void __fastcall CCNetBiosDlg::UseMessage(UINT uMsg)   
{   
MessageBox("UseMessage");
   while(p_Buffer_In==p_Buffer_Out)   
       {
          m_sreceive=(CString)Buffer[p_Buffer_Out].text;
  m_creceive.SetWindowText(m_sreceive);
          p_Buffer_Out++;               
          if(p_Buffer_Out==50)
          p_Buffer_Out=0;
        }
}
其中ZFS定义为:struct  ZFS  {char  text[20];};  
                             struct  ZFS  Buffer[50];  
谁有这方面的VC6的代码?只要求用NetBios来发送和接受数据就行。  

解决方案 »

  1.   

    =============我在VC中写的代码======================  
    bool  CNetBios::Send(CString  callname,CString  *SendData,int  len)  
    {
    ...
    }改为bool  CNetBios::Send(char* callname, char* SendData,int  len)
    {
    ...
    }
      

  2.   

    或改为:
    bool  CNetBios::Send(CString  callname, CString SendData, int len)
    {
    ...
    }
      

  3.   

    bool  CNetBios::Send(CString  callname,CString  *SendData,int  len)  
    memcpy(ncb.ncb_callname,callname,strlen(callname));  
    ...bcb里面好好的char* 干嘛要改成CString?CString的用法不熟的话,最好还是
    改回char* 吧。
      

  4.   

    everandforever(Forever):我用VC做的,要用到文本框控件输入发送的数据,那数据很定是CString类型,怎么转换成char*啊?
      

  5.   

    needways(天涯浪子):将参数改为bool  CNetBios::Send(CString  callname, CString SendData, int len)
    {
    ...
    }不行啊?因为ncb.ncb_buffer必须用unsigned char 类型的啊?你的两中方法我都试过了,不能解决问题啊?
      

  6.   

    bool  CNetBios::Send(char* callname, char* SendData,int  len)
    {
    ...
    ncb.ncb_buffer=(unsigned char*)SendData;
    ...
    }或bool  CNetBios::Send(CString  callname, CString SendData, int len)
    {
    ...
    ncb.ncb_buffer=(unsigned char*)(LPCTSTR)SendData;
    ...
    }
      

  7.   

    needways(天涯浪子):谢谢!你帮我解决了一小部分的问题,现在用我自己做的程序发给一个用C++ Builder6做的程序能正确发送,但我用VC做的问题还是没有解决,没法接受到数据并且显示在一个文本对话框的控件中(控件变量名为m_sreceive).现将我的VC代码全贴出来如下:
    ===================NetBios类的头文件==============================
    #include "nb30.h"class CNetBios  
    {
    public:
       CNetBios();
           virtual ~CNetBios();

    public :
            NCB ncb;
    CString *ReceiveBuffer[50]; //ÕâÀïÉèÿ¸öÊý¾Ý±¨×î´ó²»³¬¹ý50¸ö×Ö½Ú
            LANA_ENUM lnum;
    public  :
            int uselana;
            UCHAR ncbnum;          //×¢²áÃûË÷ÒýÖµ
           bool ResetAll(LANA_ENUM *lenum,UCHAR ucMaxSession ,
                    UCHAR ucMaxName,BOOL bFirstName);
            bool Reset(UCHAR ucMaxSession ,UCHAR ucMaxName,
        BOOL bFirstName);
            bool AddName(CString name);    //×¢²á±ðÃû   nameΪÒÔ\0½áÊøµÄ×Ö·û´®
           bool DelName(char *name);    //ɾ³ý±ðÃû   nameΪÒÔ\0½áÊøµÄ×Ö·û´®
           bool Send(CString callname,CString *SendData,int len);    //·¢ËÍÊý¾Ý¨£¬±ðÃûͬÉÏ£¬Öмä²ÎÊýΪ·¢ËÍÊý¾ÝµÄÖ¸Õë¡£
           bool LanaEnum(LANA_ENUM *lenum);     //½ÓÊÕÊý¾Ý±¨
            bool Receive(void  (__stdcall post)(NCB *ncb));
    };
    ===============NetBios类的实现文件======================
    // NetBios.cpp: implementation of the CNetBios class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "CNetBios.h"
    #include "NetBios.h"
    #pragma comment(lib,"netapi32.lib")
    #pragma comment(lib,"ws2_32.lib")
    #ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////CNetBios::CNetBios()
    {
       ncbnum=0;             //³õʼ»¯×¢²áÃûË÷ÒýÖµ
       uselana=0;
    }CNetBios::~CNetBios()
    {}
    bool CNetBios::ResetAll(LANA_ENUM *lenum,UCHAR ucMaxSession,
                    UCHAR ucMaxName,BOOL bFirstName)
    {
            int i; 
            ZeroMemory(&ncb,sizeof(NCB));
            ncb.ncb_command=NCBRESET;
            ncb.ncb_callname[0]=ucMaxSession;
            ncb.ncb_callname[2]=ucMaxName;
            ncb.ncb_callname[3]=(UCHAR)bFirstName;        for(i=0;i<lenum->length;i++)
            {
                    ncb.ncb_lana_num=lenum->lana[i];
                    if(Netbios(&ncb)!=NRC_GOODRET)
                            return false;
            }
            return true;
    }
    bool CNetBios::Reset(UCHAR ucMaxSession,UCHAR ucMaxName,BOOL bFirstName)
    {
        
       ZeroMemory(&ncb,sizeof(NCB));
    //memset(&ncb,0,sizeof(NCB));
       ncb.ncb_command=NCBRESET;
       ncb.ncb_callname[0]=ucMaxSession;
       ncb.ncb_callname[2]=ucMaxName;
       ncb.ncb_callname[3]=(UCHAR)bFirstName;
       ncb.ncb_lana_num=uselana;
       if(Netbios(&ncb)!=NRC_GOODRET)
          return false;
       return true;
    }
    bool CNetBios::AddName(CString name)   //&Atilde;ü&Atilde;&ucirc;
    {
       bool result=false;
       ZeroMemory(&ncb,sizeof(NCB));
       ncb.ncb_command=NCBADDNAME;
       memcpy(ncb.ncb_name,name,strlen(name));
       ncb.ncb_lana_num=uselana;
       if(Netbios(&ncb)==NRC_GOODRET)
       {
          ncbnum=ncb.ncb_num; //×&cent;&sup2;á&ordm;&Atilde;&pound;&not;±&pound;&acute;&aelig;×&cent;&sup2;á&Atilde;&ucirc;&Euml;÷&Ograve;&yacute;&Ouml;&micro;
          result=true;
       }
       return result;
    }bool CNetBios::Send(CString callname,CString *SendData,int len)
    {
      bool result=false;
      if(ncbnum)
      {
         ZeroMemory(&ncb,sizeof(NCB));
      //memset((char*)&ncb,0,sizeof(NCB));
         ncb.ncb_command=NCBDGSEND;       //&Iacute;&not;&sup2;&frac12;&Ecirc;&yacute;&frac34;&Yacute;±¨·&cent;&Euml;&Iacute;
         ncb.ncb_num=ncbnum;
         ncb.ncb_buffer=(unsigned char *)SendData;
         ncb.ncb_length=len;
     
         memcpy(ncb.ncb_callname,callname,strlen(callname));
         ncb.ncb_lana_num=uselana;
         if(Netbios(&ncb)==NRC_GOODRET)
         {
             result=true;
         }
       }
       return result;
    }
    bool CNetBios::Receive(void  (__stdcall *ncb_post)(NCB *))
    {
           bool result=false;       if(ncbnum)
           {
            memset(&ncb,0,sizeof(NCB));
            ncb.ncb_command=NCBDGRECV|ASYNCH;//&Ograve;ì&sup2;&frac12;&frac12;&Oacute;&Ecirc;&Otilde;
            ncb.ncb_num=ncbnum;
            ncb.ncb_buffer=(unsigned char *)ReceiveBuffer;
            ncb.ncb_length=50;  //&Agrave;à&Ouml;&ETH;&Eacute;è&Ouml;&Atilde;&micro;&Auml;&frac12;&Oacute;&Ecirc;&Otilde;&raquo;&ordm;&sup3;&aring;&Ccedil;&oslash;&micro;&Auml;&acute;ó&ETH;&iexcl;
     
    //ncb_post(&ncb);
    ncb.ncb_post=ncb_post;//(&ncb);
            ncb.ncb_lana_num=uselana;
            if(Netbios(&ncb)==NRC_GOODRET)
            {
              result=true;
            }
           }
           return result;
    }
    bool CNetBios::LanaEnum(LANA_ENUM * lenum)
    {
          bool result=false;     ZeroMemory(&ncb,sizeof(NCB));
            //memset(&ncb,0,sizeof(NCB));
            ncb.ncb_command=NCBENUM;
            ncb.ncb_buffer=(unsigned char*)lenum;
            ncb.ncb_length=sizeof(LANA_ENUM);        Netbios(&ncb);
            if(ncb.ncb_cmd_cplt==NRC_GOODRET)
                    result=true;
            return result;}
    bool CNetBios::DelName(char *name)        //&Eacute;&frac34;&sup3;&yacute;&Atilde;&ucirc;×&Ouml;
    {
            bool result=false;        if(ncbnum)
            {
             memset(&ncb,0,sizeof(NCB));
             ncb.ncb_command=NCBDELNAME;
             //strcpy(ncb.ncb_name,name);
             memcpy(ncb.ncb_name,name,strlen(name));
             //ncb.ncb_name[0]=(unsigned char)(*name);
             //ncb.ncb_name[1]=0;
             ncb.ncb_lana_num=uselana;         if(Netbios(&ncb)==NRC_GOODRET)
             {
              result=true;
              ncbnum=0;
             }
            }
            return result;
    }
      

  8.   

    ================CCNetBiosDlg类的头文件===========================
    // CNetBiosDlg.h : header file
    //#if !defined(AFX_CNETBIOSDLG_H__5D2E5005_EB86_49EB_AD96_8F7CEC8272A3__INCLUDED_)
    #define AFX_CNETBIOSDLG_H__5D2E5005_EB86_49EB_AD96_8F7CEC8272A3__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    #include "netbios.h"
    /////////////////////////////////////////////////////////////////////////////
    // CCNetBiosDlg dialog
    #include "NetBios.h"
    class CCNetBiosDlg : public CDialog
    {
    // Construction
    public:
    CCNetBiosDlg(CWnd* pParent = NULL); // standard constructor
    HWND hwnd;
    private:
    CNetBios SendBios;
        CNetBios RecBios;   //&Eacute;ú&sup3;&Eacute;netbios&Ecirc;&Otilde;&Iacute;¨&ETH;&Aring;&para;&Ocirc;&Iuml;ó 
    struct ZFS {char text[20];};
        struct ZFS Buffer[50];  //&raquo;&ordm;&sup3;&aring;
        int p_Buffer_In,p_Buffer_Out; //&raquo;&ordm;&sup3;&aring;&Ouml;&cedil;&Otilde;&euml;
    public:
        void  RecPostProgram(NCB *ncb);
    //&Ecirc;&Otilde;&micro;&frac12;Netbios&Ecirc;&yacute;&frac34;&Yacute;&micro;&Auml;&Iuml;&ucirc;&Iuml;&cent;&acute;&brvbar;&Agrave;í&ordm;&macr;&Ecirc;&yacute;&para;¨&Ograve;&aring;
        void __fastcall UseMessage(UINT uMsg);
    // Dialog Data
    //{{AFX_DATA(CCNetBiosDlg)
    enum { IDD = IDD_CNETBIOS_DIALOG };
    CEdit m_creceive;
    CEdit m_cname;
    CEdit m_clabel;
    CString m_slabel;
    CString m_sname;
    CString m_slana;
    CString m_starget;
    CString m_stext;
    CString m_sreceive;
    CString m_sstatelana;
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CCNetBiosDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    HICON m_hIcon; // Generated message map functions
    //{{AFX_MSG(CCNetBiosDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnReSetLanaButton6();
    afx_msg void OnQinZhuButton3();
    afx_msg void OnZhuCeButton2();
    afx_msg void OnGetLanaButton4();
    afx_msg void OnSendButton5();
    afx_msg void OnReSetAllButton1();
    //}}AFX_MSG
    private:
          
     
    DECLARE_MESSAGE_MAP()
    };
      

  9.   

    ===================CCNetBiosDlg类的实现部分和全局函数==================
    // CNetBiosDlg.cpp : implementation file
    //#include "stdafx.h"
    #include "CNetBios.h"
    #include "CNetBiosDlg.h"
    #include "NetBios.h"#ifdef _DEBUG
    #define new DEBUG_NEW#define WM_MESSAGE (WM_USER+100)#undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CCNetBiosDlg dialogCCNetBiosDlg::CCNetBiosDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CCNetBiosDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CCNetBiosDlg)
    m_slabel = _T("");
    m_sname = _T("");
    m_slana = _T("");
    m_starget = _T("");
    m_stext = _T("");
    m_sreceive = _T("");
    m_sstatelana = _T("");
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CCNetBiosDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CCNetBiosDlg)
    DDX_Control(pDX, IDC_EDIT5, m_creceive);
    DDX_Control(pDX, IDC_EDIT2, m_cname);
    DDX_Control(pDX, IDC_EDIT6, m_clabel);
    DDX_Text(pDX, IDC_EDIT6, m_slabel);
    DDX_Text(pDX, IDC_EDIT2, m_sname);
    DDX_Text(pDX, IDC_EDIT1, m_slana);
    DDX_Text(pDX, IDC_EDIT4, m_starget);
    DDX_Text(pDX, IDC_EDIT3, m_stext);
    DDX_Text(pDX, IDC_EDIT5, m_sreceive);
    DDX_Text(pDX, IDC_EDIT7, m_sstatelana);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CCNetBiosDlg, CDialog)
    //{{AFX_MSG_MAP(CCNetBiosDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON6, OnReSetLanaButton6)
    ON_BN_CLICKED(IDC_BUTTON3, OnQinZhuButton3)
    ON_BN_CLICKED(IDC_BUTTON2, OnZhuCeButton2)
    ON_BN_CLICKED(IDC_BUTTON4, OnGetLanaButton4)
    ON_BN_CLICKED(IDC_BUTTON5, OnSendButton5)
    ON_MESSAGE(WM_MESSAGE, UseMessage)
    ON_BN_CLICKED(IDC_BUTTON1, OnReSetAllButton1)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CCNetBiosDlg message handlers
    CCNetBiosDlg *CNetBiosDlg;BOOL CCNetBiosDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here
    //////////////////////////////////
     OSVERSIONINFO osvi;
         osvi.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
         GetVersionEx(&osvi);  if(osvi.dwPlatformId==1&&osvi.dwMajorVersion==4)//win98
      {
            SendBios.uselana=0;
            RecBios.uselana=0;
      }
      else if(osvi.dwPlatformId==2&&osvi.dwMajorVersion==5&&osvi.dwMinorVersion==0)//win2000
      {
            SendBios.uselana=3;
            RecBios.uselana=3;
      }
      else if(osvi.dwPlatformId==2&&osvi.dwMajorVersion==5&&osvi.dwMinorVersion==1)//win_xp
      {
            SendBios.uselana=0;
            RecBios.uselana=0;
      }
      CString str;
      str.Format("%d",SendBios.uselana);
      m_sstatelana=str;
      SendBios.LanaEnum(&SendBios.lnum);
       p_Buffer_In=0;              //&frac12;&Oacute;&Ecirc;&Otilde;&raquo;&ordm;&sup3;&aring;&Ouml;&cedil;&Otilde;&euml; &ordm;&Iacute;&para;&Aacute;&Egrave;&iexcl;&Ouml;&cedil;&Otilde;&euml;&Ccedil;&aring;&Aacute;&atilde;
       p_Buffer_Out=0;
       for(int j=0;j<50;j++)
       for(int g=0;g<20;g++)
       Buffer[j].text[g]=' ';
    ///////////////////////////////////

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CCNetBiosDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CCNetBiosDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.