Tag Archives: C++

Be Careful With Backslash in WMI Query String

Background & Issue:

Recently, a customer encountered a issue:

He removed a volume’s drive letter which label was “AAA” in the Disk Management(Win+R -> diskmgmt.msc)

Now, getting the free space of this volume is what he wanted, as we know, each volume has GUID to identify itself, so this customer create a method to obtain volume GUID by label, it used WMI query:

internal string GetDriveGuidByLabel(string driveLabel)
{
            using (ManagementObjectSearcher ms = new ManagementObjectSearcher(String.Format("Select * From Win32_Volume Where Label = '{0}'", driveLabel)))
            {
                foreach (var mo in ms.Get().Cast<ManagementObject>())
                {
                    return mo["DeviceID"].ToString();
                }
            }
            return null;
}

The result like this: \\?\Volume{2c6c2c33-987b-11e3-8257-806e6f6e6963}\

Next, after getting the volume GUID, to retrieve the information of this volume, WMI query could accomplish this:

SELECT * FROM Win32_Volume WHERE DeviceID = '\\\\?\\Volume{2c6c2c37-987b-11e3-8257-806e6f6e6963}\\'

He created a method:

internal Int64 GetDriveAvailableSpaceByDriveGuid(string driveGuid)
{
            // The commented line below does not work - returns InvalidQuery System.Management.Exception no matter what I tried.
            using (ManagementObjectSearcher ms = new ManagementObjectSearcher(String.Format("Select * from Win32_Volume Where DeviceID = '{0}'", driveGuid)))
            {
                ManagementObject mo = ms.Get().Cast<ManagementObject>().First();
                return Int64.Parse(mo["FreeSpace"].ToString());
            }
            return 0;
}

But it didn’t work with exception as below:

An unhandled exception of type ‘System.Management.ManagementException’ occurred in System.Management.dll
Additional information: Invalid query

Troubleshoot & Solution:

At first, I used WMI Code Creator to verify WMI query string: Utility Spotlight: WMI Code Creator

The WMI query string was correct. I noticed that there were some backslashes inside the volume GUID, generally, we need to use a verbatim string literal @”…” to prevent the backslash being treated as an escape sequence in C#, like this:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_Volume WHERE DeviceID = '\\\\?\\Volume{2c6c2c37-987b-11e3-8257-806e6f6e6963}\\'");

For this customer’s method, we need to use String.Replace() method for avoiding exception:

internal Int64 GetDriveAvailableSpaceByDriveGuid(string driveGuid)
{
            using (ManagementObjectSearcher ms = new ManagementObjectSearcher(String.Format("Select * from Win32_Volume Where DeviceID = '{0}'", driveGuid.Replace("\\", @"\\"))))
            {
                ManagementObject mo = ms.Get().Cast<ManagementObject>().First();
                return Int64.Parse(mo["FreeSpace"].ToString());
            }

            
            return 0;
}

Screenshot:
GetDriveAvailableSpaceByDriveGuid

Always Use Plain Type(e.g. char*) As Parameter For P/Invoke

Background:

Recently, a customer encountered a strange issue. He created a C++ class library with I/O operation:

Source File:

#include "stdafx.h"
#include
#include

#include "DynamicDLLToCall.h"
using namespace std;

void zlog(string fstr)
{
	FILE * debug;
	debug = fopen("zlog.txt", "a");
	ifstream iFile(fstr);
	fprintf(debug, "fsadfsdfsdfsdfdsfsdf");
	fclose(debug);
}

Header File:

using namespace System;
using namespace std;

extern "C" __declspec(dllexport) void zlog(string fstr);

Now, he want to call zlog() method in a .NET project:

[DllImport("D:/ClassLibrary1.dll")]
public static extern void zlog(string fstr);//imported func

private void Button_Click(object sender, RoutedEventArgs e)
{
            zlog("asdas");
}

Issue:
This app crashed when the zlog() function is called and the zlog.txt file was successfully created.

Troubleshoot:

At first, I tried to comment some code snippets in the zlog(string fstr) function, but this issue still existed, so I think it should be more related to the declaration of this function.

With the help from C++ expert, it may be caused by this reason: C++ doesn’t support(or fully support) ABI(Application Binary Interface) which is required to help clean the stack of the object.

What is ABI? Please refer to these references:

ABIs cover details such as:

–the sizes, layout, and alignment of data types

–the calling convention, which controls how functions’ arguments are passed and return values retrieved; for example, whether all parameters are passed on the stack or some are passed in registers, which registers are used for which function parameters, and whether the first function parameter passed on the stack is pushed first or last onto the stack

–how an application should make system calls to the operating system and, if the ABI specifies direct system calls rather than procedure calls to system call stubs, the system call numbers

–and in the case of a complete operating system ABI, the binary format of object files, program libraries and so on.

A complete ABI, such as the Intel Binary Compatibility Standard (iBCS),[1] allows a program from one operating system supporting that ABI to run without modifications on any other such system, provided that necessary shared libraries are present, and similar prerequisites are fulfilled.

——–

So always use plain type(e.g. char*) as parameter for P/invoke instead of using type within the std namespace as parameter.

The following code worked well now:

C++ declaration:

extern "C" __declspec(dllexport) void zlog(char* fstr);
1

C++ function:
1
void zlog(char* fstr)
{
 //...your code
}

In .NET application code behind:

[DllImport("DynamicDLLToCall", CallingConvention = CallingConvention.Cdecl)]
public static extern void zlog(string fstr);