在Visual C ++中,我有一个如下所示的函数。它是一个DLL代码。从vb6应用程序我调用DLL并运行该函数并获得result.But每次当我从我的vb6应用程序调用它(没有关闭vb6 exe)时,如果exe未关闭,我得到带有最后结果的附加值的结果。(例如,当我运行的第一个结果是" a"第二次是" aa"依此类推。)因此我认为这是由于BSTR消息未被设置为NULL这个功能的开头。 下面是VC ++中的代码。那么如何在开始时将BSTR设置为null或为空或如何解决我的错误?
BSTR __stdcall getHardDriveComputerID (short* disk_cnt , short* method)
{
BSTR Message=NULL;
int i,length;
size_t len = 0;
int done = FALSE;
__int64 id = 0;
OSVERSIONINFO version;
strcpy (HardDriveSerialNumber, "");
memset (&version, 0, sizeof (version));
version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
GetVersionEx (&version);
if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
// this works under WinNT4 or Win2K if you have admin rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf ("\nTrying to read the drive IDs using physical access with admin rights\n");
#endif
done = ReadPhysicalDriveInNTWithAdminRights ();
*disk_cnt=hard_disk_cnt;
*method=1;
if ( ! done)
{
done = ReadIdeDriveAsScsiDriveInNT ();
*disk_cnt=hard_disk_cnt;
*method=2;
}
if ( ! done)
{done = ReadPhysicalDriveInNTWithZeroRights ();
*disk_cnt=hard_disk_cnt;
*method=3;
if ( ! done)
{ done = ReadPhysicalDriveInNTUsingSmart ();
*disk_cnt=hard_disk_cnt;
*method=4;
}
}
else
{
// this works under Win9X and calls a VXD
int attempt = 0;
// try this up to 10 times to get a hard drive serial number
for (attempt = 0;
attempt < 10 && ! done && 0 == HardDriveSerialNumber [0];
attempt++)
done = ReadDrivePortsInWin9X ();
*disk_cnt=hard_disk_cnt;
*method=5;
}
if (HardDriveSerialNumber [0] > 0)
{
char *p = HardDriveSerialNumber;
WriteConstantString ("HardDriveSerialNumber", HardDriveSerialNumber);
// ignore first 5 characters from western digital hard drives if
// the first four characters are WD-W
if ( ! strncmp (HardDriveSerialNumber, "WD-W", 4))
p += 5;
for ( ; p && *p; p++)
{
if ('-' == *p)
continue;
id *= 10;
switch (*p)
{
case '0': id += 0; break;
case '1': id += 1; break;
case '2': id += 2; break;
case '3': id += 3; break;
case '4': id += 4; break;
case '5': id += 5; break;
case '6': id += 6; break;
case '7': id += 7; break;
case '8': id += 8; break;
case '9': id += 9; break;
case 'a': case 'A': id += 10; break;
case 'b': case 'B': id += 11; break;
case 'c': case 'C': id += 12; break;
case 'd': case 'D': id += 13; break;
case 'e': case 'E': id += 14; break;
case 'f': case 'F': id += 15; break;
case 'g': case 'G': id += 16; break;
case 'h': case 'H': id += 17; break;
case 'i': case 'I': id += 18; break;
case 'j': case 'J': id += 19; break;
case 'k': case 'K': id += 20; break;
case 'l': case 'L': id += 21; break;
case 'm': case 'M': id += 22; break;
case 'n': case 'N': id += 23; break;
case 'o': case 'O': id += 24; break;
case 'p': case 'P': id += 25; break;
case 'q': case 'Q': id += 26; break;
case 'r': case 'R': id += 27; break;
case 's': case 'S': id += 28; break;
case 't': case 'T': id += 29; break;
case 'u': case 'U': id += 30; break;
case 'v': case 'V': id += 31; break;
case 'w': case 'W': id += 32; break;
case 'x': case 'X': id += 33; break;
case 'y': case 'Y': id += 34; break;
case 'z': case 'Z': id += 35; break;
}
}
}
id %= 100000000;
if (strstr (HardDriveModelNumber, "IBM-"))
id += 300000000;
else if (strstr (HardDriveModelNumber, "MAXTOR") ||
strstr (HardDriveModelNumber, "Maxtor"))
id += 400000000;
else if (strstr (HardDriveModelNumber, "WDC "))
id += 500000000;
else
id += 600000000;
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf ("\nHard Drive Serial Number__________: %s\n",
HardDriveSerialNumber);
printf ("\nHard Drive Model Number___________: %s\n",
HardDriveModelNumber);
printf ("\nComputer ID_______________________: %I64d\n", id);
#endif
char tempdisk_serials[]="";
for(i = 0 ; i<=hard_disk_cnt ; i++)
{
if(hard_disk_serial[i] != "")
{
strcat(tempdisk_serials, hard_disk_serial[i]);
length = strlen(tempdisk_serials);
/* Check if we need to insert newline */
if(tempdisk_serials[length-1] != '\n')
{
tempdisk_serials[length] = '\n'; /* Append a newline */
tempdisk_serials[length+1] = '\0'; /* followed by terminator */
}
}
}
//Converting char[] to BSTR
Message = SysAllocStringByteLen (tempdisk_serials, strlen(tempdisk_serials));
return (BSTR) Message;
}
此修改后的代码显示在链接http://www.winsim.com/diskid32/diskid32.cpp
中修改
实际上它与tempdisk_serials没有问题。这个变量是从hard_disk_serial设置的。它全局声明为char hard_disk_serial [16] [1024];(我的问题中的代码中未显示)。如何清除我的函数中的这个变量?我试过hard_disk_serial [] [] = {0};它会给出语法错误。