Category Archives: C#

More adventures with C++/CLI

I’ve been working with C++/CLI to get access to native API functionality from C# some more lately. I’ve been running into cases where something compiles flawlessly when I create the project but then fails miserably when pulled from GIT clean and rebuilt. I’m looking at various build issues that might be contributing to this and will hopefully find the right recipe soon.

Some notes here related to parts that matter and APIs that I’m looking to use…half of this for my own reference as I mess with solutions to see what works:

Marshaling strings the (relatively) easy way:

 #include <msclr/marshal.h>

System::String^ myPath = "ABC";
std::wstring path = msclr::interop::marshal_as(myPath);

File information:

typedef struct _FILE_ID_INFO {
ULONGLONG VolumeSerialNumber;
FILE_ID_128 FileId;


BOOL ok = GetFileInformationByHandleEx(myHandle, FileIdInfo, &info, sizeof(info));

Alternate string marshaling:

inline void MarshalString(String ^ s, std::wstring& os) {
using namespace Runtime::InteropServices;
const wchar_t* chars =
(const wchar_t)Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars;

Volume information:

BOOL ok = GetVolumeInformationW(path.c_str(),
volumeNameBuffer, MAX_PATH + 1,
filesSystemName, MAX_PATH + 1);

File attributes (can be used on c:\ to find things like volume creation time stamp)

BOOL ok(GetFileAttributesExW(path.c_str(),
static_cast<void *>(&info)));

creationDate = gcnew System::DateTime((long long)(info.ftCreationTime.dwLowDateTime) | ((long long)(info.ftCreationTime.dwHighDateTime) << 32));

Additional volume information:

    wchar_t volumeName[1024];     wchar_t fsName[1024];     DWORD vsn;     DWORD fsflags;     BOOL ok(GetVolumeInformationW(path.c_str(),         volumeName, 1024,         &vsn,         nullptr,         &fsflags,         fsName, 1024));


    ULARGE_INTEGER free;     ULARGE_INTEGER total;     ULARGE_INTEGER totalfree;     BOOL ok(GetDiskFreeSpaceExW(path.c_str(), &free, &total, &totalfree));

and basic info

BOOL ok(GetFileInformationByHandleEx(handle, FileBasicInfo, static_cast(&info), sizeof(info)));

Started adding mongodb support to C# code

I started adding in concrete MongoDB support to the tool code I’m building. Last night I incorporated the drivers into the solution and set up the connection code for the database. My initial attempt to directly use the C++/CLI object to push a record into the database failed but I expect to add a class that is properly serializable to BSON tonight and get volume information persistence working. I still need to get a better handle on updates in MongoDB. I know there are various sorts of atomic test and set sorts of operations on documents and in order to make this work properly with concurrent access, I need to understand those operations and use them to ensure that updates don’t ‘walk’ on themselves.

When I have a bit more time to type, I’ll add in more details on the MongoDB driver work and the approaches I’m using.