因为我现在正在编写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
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
解决方案 »
- sp1的vs2008//为什么无法建立文档与视图的程序???
- ADO BOF问题
- IWebBrowserApp是什么类型?
- 请教ADO+ACCESS中的几个问题,不知道该如何解决.
- 为什么总是提示类未注册?
- 求“VisualC++数据库高级编程”一书的配套光盘
- 【表扬】 其实进专家榜很容易 看看自己进了
- 连接release&debug的lib为什么不一样?
- 这个vc6.0build时出现的提示是什么引起的?如何杜绝这种提示?
- 在对话框中的编辑框控件中如何响应文件拖放时的OnDropFile()函数?
- 初学vc请教:在Win98下执行dlg.DoModal();就死机而Win200下没有问题
- va_list是什么类型?
{
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;
}
{
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;
}
{
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;
}
大概如此,另外,你的程序思路可能不对。
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
但,编写dll中,应把字符类型写成 Char 不能写成 CString 否则编译不过,所以还能麻烦各位高手继续解答。谢!
{
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;
}
{
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;
}
EXPORTS
Encode
{
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;
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;}