用CString::GetData()获得数据地址,CString::GetLength()获得长度,都作为long参数传给vb,然后再vb中用CopyMemory获得String,应该可以!

解决方案 »

  1.   

    Declare Function gets Lib "testdllmfc1" () As String
    MsgBox gets()LPSTR WINAPI WINAPI gets()
    {
    return "hehe";
    }运行中报错
    string!=NULL
      

  2.   

    <<用char* 代替CString类。 怎么写呢?char* WINAPI gets(){}...?好像不行。
      

  3.   

    加到150分。怎么把char *传给VB?谁能给出例子,感激不尽!
      

  4.   

    我未向VB中传过,但是向PB中可以直接传引用的,如果不可以的话,用拷贝内存数据块的形式应该可以的,请参考我的下面的函数,注释掉的部分是另外一种写法,较麻烦,但是适用于任何类型:
    BOOL __stdcall GetFileTimer(LPCTSTR filename,void *ptime)
    {
        HANDLE hfile;//定义的存放文件句柄
        SYSTEMTIME lt;//标准系统时间结构变量
    FILETIME createtime,accesstime,lastwritetime;
    //获得文件句柄只能用这种方式,有时可以用FileOpen()得HFILE型句柄
    hfile=::CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY,0);
    ::GetFileTime(hfile,&createtime,&accesstime,&lastwritetime);
        ::FileTimeToSystemTime(&lastwritetime,&lt);
    short * npTime = ( short * ) ptime;
    //int nTemp =  ;
        
        
    npTime[0]=lt.wYear;
    npTime[1]=lt.wMonth ;
    npTime[2]=lt.wDay;
    npTime[3]=(lt.wHour+8)%24;
    npTime[4]=lt.wMinute ;
    npTime[5]= lt.wSecond ;/* memcpy ( npTime, & nTemp, sizeof ( int ) );
    npTime ++;
    nTemp = lt.wMonth ;
    memcpy ( npTime, & nTemp, sizeof ( int ) );
    nTemp = lt.wDay;
    npTime ++;
    memcpy ( npTime, & nTemp, sizeof ( int ) );
    nTemp = lt.wHour;
    npTime ++;
    memcpy ( npTime, & nTemp, sizeof ( int ) );
    nTemp = lt.wMinute ;
    npTime ++;
    memcpy ( npTime, & nTemp, sizeof ( int ) );
    nTemp = lt.wSecond ;
    */
    return TRUE;
    }
      

  5.   

    你可以通过参数返回值,在VB中分配空间,然后把空间指针作为参数传递给函数,这样便于管理空间,不容易发生内存泄漏。void WINAPI gets(LPSTR pCh)
    {
        sprintf(pch,"%s", "hehe");
    }
      

  6.   

    不好意思插个广告http://www.csdn.net/expert/topic/129/129452.shtm
      

  7.   

    To greensoft(一个学习很棒的初中生。) :来我的贴子里发广告,交保护费了么?回答你,当然先学windows编程了,消息机制怎么可以不看?不过我觉得你先把中考忙完了再说其它:)。
      

  8.   

    To eggplant(拉拉)兄:
    /*------------------------------*/
    你可以通过参数返回值,在VB中分配空间,然后把空间指针作为参数传递给函数,这样便于管理空间,不容易发生内存泄漏。void WINAPI gets(LPSTR pch)
    {
        sprintf(pch,"%s", "hehe");

    /*------------------------------*/
    这么做了,可是编译报错:'void __stdcall gets(char *)' 
    --------------------Configuration: testdllmfc1 - Win32 Debug--------------------
    Compiling...
    testdllmfc1.cpp
    D:\Documents and Settings\Administrator\桌面\testdllmfc1\testdllmfc1.cpp(86) : error C2556: 'void __stdcall gets(char *)' : overloaded function differs only by return type from 'char *__cdecl gets(char *)'
    d:\program files\microsoft visual studio\vc98\include\stdio.h(324) : see declaration of 'gets'
    D:\Documents and Settings\Administrator\桌面\testdllmfc1\testdllmfc1.cpp(86) : error C2373: 'gets' : redefinition; different type modifiers
        
    d:\program files\microsoft visual studio\vc98\include\stdio.h(324) : see declaration of 'gets'
    D:\Documents and Settings\Administrator\桌面\testdllmfc1\testdllmfc1.cpp(86) : error C2491: 'gets' : definition of dllimport function not allowed
    Error executing cl.exe.testdllmfc1.dll - 3 error(s), 0 warning(s)
      

  9.   

    不好意思,原来和C语言重名了呀。编译通过,但运行报错:string!=NULLvoid WINAPI gets(LPSTR pch)
    {
        sprintf(pch,"%s", "hehe");
    } /*------------------------------*/Dim teststr As StringPrivate Sub Command3_Click()
    getstr (teststr)
    MsgBox teststr
    End Sub
      

  10.   

    该成/*------------------------------*/Dim teststr As StringPrivate Sub Command3_Click()teststr="0000000000000000"getstr (teststr)
    MsgBox teststrEnd Sub就没事了!在VB中分配空间,只能这么干么?
    teststr="0000000000000000"代码太不像话,怎么该好?
      
      

  11.   

    如果你要做一个VB兼容的DLL,那必须使用VC,VB都可以使用的类型。CString 是MFC特有的类,在这种情况下是不能使用的。只能使用LPCTSTR,以保证兼容,具体,可以看一些COM的书或IDL的书