用地址传递的方法,试一下VB里的AddressOf str,得到str的地址,然后把这个地址传给MyFun函数。

解决方案 »

  1.   

    freeghost(星子)谢谢你的回复。但我是要问在VC中怎么写MyFun(),谢谢。
      

  2.   

    我知道要用到什么 BSTR ,LPCTSTR之类的,但具体怎么用法不太清楚,还请高手指点一二。
      

  3.   

    你在VB中定义这个dll函数的时候用ByRef型的,在VC中用LPSTR或char *这两个是相同的,如果用LPCTSTR我想不行吧,但也不敢确定我也没试过。
      

  4.   

    freeghost(星子):理论上我也知道一点点,但在实际操作中不行:大家看我写的这个有没有问题:
    #include <comutil.h>
    extern "C" BSTR _stdcall MyFun(BSTR str)
    {
       CString str;
       str=_com_util::ConvertBSTRToString(str);
       str+="Hello,World!";
       return _com_util::ConvertStringToBSTR(str);
    }这样写的错误很多啊,请问应该怎么改?
      

  5.   

    程序是这样写,编译时会出现:
    extern "C" BSTR _stdcall MyFun(BSTR str)
    {
    CString cstr;
    cstr=_com_util::ConvertBSTRToString(str);
    cstr+="Hello,World!";
    return _com_util::ConvertStringToBSTR(cstr);
    }Linking...
       Creating library Debug/dll.lib and object Debug/dll.exp
    dll.obj : error LNK2001: unresolved external symbol "unsigned short * __stdcall _com_util::ConvertStringToBSTR(char const *)" (?ConvertStringToBSTR@_com_util@@YGPAGPBD@Z)
    dll.obj : error LNK2001: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(unsigned short *)" (?ConvertBSTRToString@_com_util@@YGPADPAG@Z)
    Debug/dll.dll : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.dll.dll - 3 error(s), 0 warning(s)这些是什么错误?
      

  6.   

    你试一下把前边的extern "C" 去掉然后再试一下呢?
      

  7.   

    请在函数开头加
    USES_CONVERSION;extern "C" BSTR _stdcall MyFun(BSTR str)
    {
      USES_CONVERSION;
      LPTSTR x = OLE2T(str);
    }
      

  8.   

    你如此声明函数:
    TYPE_declspec(dllexport) MyFun(BSTR str)
    {
     ...
     ...
    }
    其中,大写的TYPE表示是函数的类型,可以是各种类型的,如:void,int 等!
      

  9.   

    去掉对_com_util::ConvertStringToBSTR函数的引用,改用#include <comdef.h>extern "C" BSTR _stdcall MyFun(BSTR str)
    {
        _bstr_t bstrTemp(str, true);
        bstrTemp += "hello, world";
        return SysAllocString(bstrTemp);
    }