Without entering into too much details, I want to workaround a bug I’m having in Windows 10.
Through a debugging session in Windbg, I found that I need to call dsreg!DsrCmdCleanupAccountsHelper::CleanupAccounts
This is a system library and I don’t have the .lib.
I would like to call this function in C#.
I know how to Pinvoke functions. The difficulty is that it is inside a Class and it seams to be a static function (not 100% sure yet) and I don’t find the correct syntax
[DllImport("dsreg.dll", SetLastError = true)]
static extern Int32 CleanupAccount();
Obviously, this does not work because it’s inside a class.
Then I try to use LoadLibrary and GetProcAddress.
// Load the DLL
IntPtr hModule = LoadLibrary("dsreg.dll");
if (hModule == IntPtr.Zero)
{
throw new Exception("Failed to load DLL.");
}
// Get the function address using the mangled name
IntPtr pFunction = GetProcAddress(hModule, "DsrCmdCleanupAccountsHelper::CleanupAccount");
Again,this does not work.
Then I try to create a Cpp wrapper:
.h File
#ifdef __cplusplus
extern "C" {
#endif
extern __declspec(dllexport) DsrCmdCleanupAccountsHelper* CreateClassName();
extern __declspec(dllexport) void DisposeClassName(DsrCmdCleanupAccountsHelper* a_pObject);
extern __declspec(dllexport) void CleanupAccount(DsrCmdCleanupAccountsHelper* a_pObject);
#ifdef __cplusplus
}
#endif
cpp File:
#include "pch.h"
#include "DllWrapper.h"
DsrCmdCleanupAccountsHelper* CreateClassName()
{
return new DsrCmdCleanupAccountsHelper();
}
void DisposeClassName(DsrCmdCleanupAccountsHelper* a_pObject)
{
if (a_pObject != NULL)
{
delete a_pObject;
a_pObject = NULL;
}
}
void CleanupAccount(DsrCmdCleanupAccountsHelper* a_pObject)
{
if (a_pObject != NULL)
{
a_pObject->CleanupAccount();
}
}
Now the problem is that I don’t have the lib and therefore, it raises unresolved external symbols.