因为我现在正在编写dll,但对vc不熟,所以望高手把下面的函数改写成vc中dll中的函数!先谢!
Public Function Encode(Data As String, Optional Depth As Integer) As StringDim TempChar As String
Dim TempAsc As Integer
Dim NewData As String
Dim vChar As IntegerFor vChar = 1 To Len(Data)
    TempChar = Mid$(Data, vChar, 1)
        TempAsc = Asc(TempChar)
        If Depth = 0 Then Depth = 40 'DEFAULT DEPTH
        If Depth > 254 Then Depth = 254        TempAsc = TempAsc + Depth
        If TempAsc > 255 Then TempAsc = TempAsc - 255
        TempChar = Chr(TempAsc)
        NewData = NewData & TempChar
Next vChar
Encode = NewData

解决方案 »

  1.   

    CString _stdcall Encode(CString Data,int Depth=0)
    {
    char TempChar;
    CString NewData;
    int vChar; for(vChar=0;vChar<Data.GetLength();vChar++)
    {
    TempChar=Data.GetAt(vChar);

    if(Depth == 0) Depth=40;
    if(Depth > 254) Depth=254; TempChar+=Depth;
    NewData+=TempChar;
    } return NewData;
    }
      

  2.   

    CString _stdcall Encode(CString Data,int Depth=0)
    {
    char TempChar;
    CString NewData;
    int vChar; for(vChar=0;vChar<Data.GetLength();vChar++)
    {
    TempChar=Data.GetAt(vChar);

    if(Depth == 0) Depth=40;
    if(Depth > 254) Depth=254; TempChar+=Depth;
    NewData+=TempChar;
    } return NewData;
    }
      

  3.   

    CString Encode(CString Data, int  Depth ) 
    {
    char TempChar ;
    CString NewData ;
    int vChar ;
    for(vChar = 1;vChar<=Data.GetLength();vChar++)
    {
        TempChar = Data.GetAt(vChar);
            if(Depth == 0)  Depth = 40 ;
            if(Depth > 254) Depth = 254;        TempChar += Depth;
            if (TempChar > 255)  TempChar -= 255;
            NewData += &TempChar; //这一句不一定对
    } return  NewData;
    }
    大概如此,另外,你的程序思路可能不对。
      

  4.   

    File->New->Projects->Win32 Dynamic-link library->A DLL that exports some symbols
    File->New->Files->TextFile->查入a.def文件不可少)
    a.def的内容编辑如下:
    LIBRARY          "a"
    DESCRIPTION     'a.dll'  
    EXPORTS
      funA
    在a.h中
    int WINAPI Encode(int x,int y);
    在a.cpp中
    CString WINAPI Encode(CString Data,int Depth)
    {CString TempChar; 
    int TempAsc ;
    CString NewData;
    int vChar;for (vChar = 1 ;i< Len(Data);i++){
        TempChar = Data.Mid(vChar, 1)
            TempAsc = Asc(TempChar)
            If (Depth) == 0 Depth = 40 ;//'DEFAULT DEPTH
            If (Depth > 254) Depth = 254        TempAsc = TempAsc + Depth
            If (TempAsc > 255) TempAsc = TempAsc - 255
            TempChar = Chr(TempAsc)
            NewData = NewData & TempChar//可能是&&,试一下
           }return NewData;
    }
    编译成a.dll->copy a.dll c:\windows\system
    在vb的Modules(不能在form中)中声明如下:
    Declare Function Encode Lib "c:\windows\system\a.dll" (Byval x as CString,Byval y as integer) as CString
      

  5.   

    对于各位的回答,先谢谢了。
    但,编写dll中,应把字符类型写成  Char 不能写成 CString 否则编译不过,所以还能麻烦各位高手继续解答。谢!
      

  6.   

    oracle3 说得一点也没有错,用mfc就不存在这个问题,但我现在写的是win32的dll 不是mfc的dll,所以才有上面的问题
      

  7.   

    bool Encode(char* Data,int DataLen, char* szReturnData, int nReturnDataLen, int  Depth ) 
    {
    char TempChar ;if( DataLen > nReturnDataLen )
    return false;memset(szReturnData,0,nReturnDataLen );for(int i = 0 ;i<DataLen;i++)
    {
        TempChar = Data[i];
            if(Depth == 0)  Depth = 40 ;
            if(Depth > 254) Depth = 254;        TempChar += Depth;
            if (TempChar > 255)  TempChar -= 255;
            szReturnData[i] = TempChar; //这一句不一定对} 
    return  true;
    }
      

  8.   

    bool Encode(char* Data,int DataLen, char* szReturnData, int nReturnDataLen, int  Depth ) 
    {
    char TempChar ;if( DataLen > nReturnDataLen )
    return false;memset(szReturnData,0,nReturnDataLen );for(int i = 0 ;i<DataLen;i++)
    {
        TempChar = Data[i];
            if(Depth == 0)  Depth = 40 ;
            if(Depth > 254) Depth = 254;        TempChar += Depth;
            if (TempChar > 255)  TempChar -= 255;
            szReturnData[i] = TempChar; //这一句不一定对} 
    return  true;
    }
      

  9.   

    在.def文件中
    EXPORTS
      Encode
      

  10.   

    char* Encode(char* Data, int  Depth ) 
    {
    char TempChar ;
    char* NewData ;
    int vChar ;
    for(vChar = 0;vChar<strlen(Data);vChar++)
    {
        TempChar = Data[vChar];
            if(Depth == 0)  Depth = 40 ;
            if(Depth > 254) Depth = 254;        TempChar += Depth;
            if (TempChar > 255)  TempChar -= 255;
            NewData[vChar]=TempChar; } return  NewData;
    }
    大概是这个意思,但你的程序本来就有问题,这样肯定不行,我写过一段代码,比这个长的多,复杂地多!
    在vc里,你要考虑传入的参数不仅有字符串,还可能是二进制数据,因而,你必须有第二个参数指明数据的长度。
    你要根据原数据的长度计算出新数据的长度,然后对数据移位相加,得出新数据。
    新数据的长度应该是(假设原数据长度为p):long j;
    j=(p%3==0)?p/3*4:(p/3+1)*4;
      

  11.   

    以下是我写的完整的代码:
    char* _stdcall Encode(unsigned char* oribytee,long orilong)
    {   
      unsigned char* oribyte=new unsigned char[orilong+2];
        memset(oribyte,0,orilong+2);
    oribyte=oribytee;
    char* newstring=new char[];
        char* basestring="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        char newbyte[5];
    int linelen=0;
     for(long ii=0;ii<orilong;ii=ii+3)
     {  
    newbyte[0]=oribyte[ii]>>2;
    newbyte[1]=((oribyte[ii]&0x3)<<4)+((oribyte[ii+1]&0xF0)>>4);
    newbyte[2]=((oribyte[ii+1]&0xF)<<2)+((oribyte[ii+2]&0xC0)>>6);
    newbyte[3]=oribyte[ii+2]&0x3F;
    newbyte[4]=0;
    for(int g=0;g<=3;g++)
    newbyte[g]=basestring[newbyte[g]];
    linelen=linelen+1;
    if(linelen*4>74)
    {
    strcat(newstring,"\r\n");
    linelen=0;
    }
    strcat(newstring,newbyte);
    }
    if(orilong%3==1)
    {
    newstring[strlen(newstring)-1]='=';
    newstring[strlen(newstring)-2]='=';
    }
    else if(orilong%3==2)
    newstring[strlen(newstring)-1]='=';
    return newstring;}