c++ - 将 BSTR 设置为空

标签 c++ vb6 bstr

在 Visual C++ 中,我有一个如下所示的函数。它是一个 dll 代码。从 vb6 应用程序我调用 dll 并运行该函数并获得结果。但是每次当我从我的 vb6 应用程序调用它时(不关闭 vb6 exe)如果 exe 未关闭,我会得到带有最后结果附加值的结果。(例如当我运行的第一个结果是“a”,第二次是“aa”,依此类推。所以我正在使用我认为这是由于 BSTR Message 在这个函数的开头没有被设置为 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};并给出语法错误。

最佳答案

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

memset (hard_disk_serial, 0, 1024*16);

关于c++ - 将 BSTR 设置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25725384/

相关文章:

c++ - 要求用户制造的容器符合 range-v3

c++ - ATL 如何将 BSTR* str 转换为注册表项。SetValue(LPCTSTR str 类型

c - 将用 C DLL 编写的 BSTR 字符串发送到 Excel

events - 所选项目的 ListView 事件已更改

performance - 在 VB6 中计时功能/测量性能的最佳方法是什么?

c++ - COM,包含 BSTR 的 VARIANT。谁分配?

c++ - std::unordered_map:多线程插入?

c++ - 使用匿名结构初始化会在堆栈上放置额外的拷贝吗?

c++ - Windows Forms应用程序中的链接器错误

.net - 解析 .NET 中的 VB6 代码