将BSTR设置为null

Set BSTR to null

本文关键字:null 设置 BSTR      更新时间:2023-10-16

在Visual C++中,我有一个如下所示的函数。它是一个dll代码。从vb6应用程序,我正在调用dll并运行该函数并获取结果。但每次当我从我的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 rightsn");
#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__________: %sn", 
       HardDriveSerialNumber);
printf ("nHard Drive Model Number___________: %sn", 
       HardDriveModelNumber);
printf ("nComputer ID_______________________: %I64dn", 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] = '';           /* 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};并给出了语法错误。

要清除缓冲区,请使用memset函数doc

memset (hard_disk_serial, 0, 1024*16);