VB VC 混合编程中,VC 问题 --> USHORT vbString2pszString(LPVOID InBuffer, LPVOID OutBuffer, USHORT BufferLen)

shaosx 2004-08-03 08:41:20
count 是干什么的?


// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
USHORT vbString2pszString(LPVOID InBuffer, LPVOID OutBuffer, USHORT BufferLen)
{
//LPVOID : A 32-bit pointer to an unspecified type.
//USHORT : Unsigned SHORT.
char *vbString;
char *pszString;
int count=0;

vbString = (char*)InBuffer;
pszString = (char*)OutBuffer;

//memset : Sets buffers to a specified character.
//void *memset( void *dest, int c, size_t count );
memset(pszString, '\0', BufferLen);

while(count<2)
{
if(*vbString != '\0')
{
count = 0;
//strcat : Append a string.
//char *strcat( char *strDestination, const char *strSource );
strcat(pszString,vbString);
}
else
{
count++;
}
vbString++;
}
return 0;
}
...全文
62 6 点赞 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
shaosx 2004-08-03
jazy:

OK!
  • 打赏
  • 举报
回复
jazy 2004-08-03
这个问题最好不要只知道个结论,要知道原因还是自己看看MSDN里对unicode字符的说明,
尤其是BSTR及其相关数据类型的帮助,内容很多,你根据自己的需要挑着看吧,
否则很难明白vb和vc对字符串的处理
  • 打赏
  • 举报
回复
shaosx 2004-08-03
也就是说,
我对 数据结构 不太明白。

请zhuzhufox(狐狸的小猪) 指点一下。
  • 打赏
  • 举报
回复
shaosx 2004-08-03
还是不太明白!

若仅仅碰到一个'\0'怎么办?
  • 打赏
  • 举报
回复
zhuzhufox 2004-08-03
连续两个结束符表示字符串的结束(BSTR?)
Count用来计算'\0'的
  • 打赏
  • 举报
回复
shaosx 2004-08-03
// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
#include <windows.h>
#include <ole2.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
//Each implementation of C and C++ supports some features unique to its host machine or
// operating system. Some programs, for instance, need to exercise precise control over the
// memory areas where data is placed or to control the way certain functions receive
// parameters. The #pragma directives offer a way for each compiler to offer machine- and
// operating-system-specific features while retaining overall compatibility with the C and
// C++ languages. Pragmas are machine- or operating-system-specific by definition, and are
// usually different for every compiler.
#pragma pack(1)

// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
typedef struct
{
long nFlags;
long nPosition;
//LPVOID : A 32-bit pointer to an unspecified type.
LPVOID vbsPathName;
LPVOID vbsFileName;
LPVOID vbsFileType;
} vbFiles;


// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
USHORT vbString2pszString(LPVOID InBuffer, LPVOID OutBuffer, USHORT BufferLen)
{
//LPVOID : A 32-bit pointer to an unspecified type.
//USHORT : Unsigned SHORT.
char *vbString;
char *pszString;
int count=0;

vbString = (char*)InBuffer;
pszString = (char*)OutBuffer;

//memset : Sets buffers to a specified character.
//void *memset( void *dest, int c, size_t count );
memset(pszString, '\0', BufferLen);

while(count<2)
{
if(*vbString != '\0')
{
count = 0;
//strcat : Append a string.
//char *strcat( char *strDestination, const char *strSource );
strcat(pszString,vbString);
}
else
{
count++;
}
vbString++;
}
return 0;
}

// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
USHORT GetvbStringLen(LPVOID InBuffer)
{
//LPVOID : A 32-bit pointer to an unspecified type.
char *t;
int count=0;
int BufLen=0;

t = (char*)InBuffer;

while(count<2)
{
if(*t != '\0')
{
count = 0;
BufLen++;
}
else
{
count++;
}
t++;
}
return(BufLen);
}

// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
USHORT pszString2vbString(LPVOID *OutBuffer, LPVOID InBuffer, USHORT BufLen)
{
int count=0;
char * temp;
char * temp1;
char * pszString;

temp=(char*)malloc((BufLen*2)+1);
memset(temp,'\0',((BufLen*2)+1));
temp1=temp;
pszString = (char*)InBuffer;

for (count = 0; count < BufLen; count++)
{
memset(temp1,pszString[count],1);
temp1++;
temp1++;
}

memcpy(*OutBuffer, temp, ((BufLen*2)+1));

return(1);
}

// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
USHORT unPackVBStr(LPVOID InBuffer, char ** OutBuffer)
{
int i;
int rc=1;

if ((i = GetvbStringLen(InBuffer)) != 0)
{
*OutBuffer=(char*)malloc((i+2)*sizeof(char));
memset(*OutBuffer,'\0',i);
vbString2pszString(InBuffer, *OutBuffer, i);
}
else
{
OutBuffer = 0;
rc = 0;
}
return rc;
}


// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
USHORT PackVBStr(char * InBuffer, LPVOID *OutBuffer)
{

int len;
int rc=1;

if (!OutBuffer)
rc = 0;
else
{
if (InBuffer)
{
len = GetvbStringLen(*OutBuffer);
pszString2vbString(OutBuffer, InBuffer, len);
}
else
pszString2vbString(OutBuffer, "", 0);
}
return rc;
}

// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
//
unsigned short _declspec(dllexport) Test(LPSAFEARRAY FAR * ppsaFiles)
{
unsigned int i;
vbFiles * Files;
char * pszPath;
char * pszFile;
char * pszType;

Files=(vbFiles*)((*ppsaFiles)->pvData);

for (i=0;i<((*ppsaFiles)->rgsabound->cElements);i++,Files++)
{
unPackVBStr(Files->vbsPathName, &pszPath);
unPackVBStr(Files->vbsFileName, &pszFile);
unPackVBStr(Files->vbsFileType, &pszType);
strcpy(pszType,"TEXT");
PackVBStr(pszType, &Files->vbsFileType);
unPackVBStr(Files->vbsFileType, &pszType);
}

return 1;
}

// ======== ======== ======== ======== ======== ======== ======== ======== ======== ========
  • 打赏
  • 举报
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2004-08-03 08:41
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……