64 Bit Custom Actions #8049
Answered
by
StephenHodgson
StephenHodgson
asked this question in
Questions
-
I have a custom action I wrote using a Wix C# library. The problem is I have a 64bit dll I need to load during the installation process but the custom action server is 32bit. I'm using My c# Custom Action class is pretty simple: public static class RegisterDllAction
{
[CustomAction]
public static ActionResult RegisterDll(Session session)
{
try
{
return InvokeDll(session.CustomActionData["DLL_PATH"], true, session) ? ActionResult.Success : ActionResult.Failure;
}
catch (Exception ex)
{
session.Log($"Error: {ex.Message}");
return ActionResult.Failure;
}
}
[CustomAction]
public static ActionResult UnRegisterDll(Session session)
{
try
{
return InvokeDll(session.CustomActionData["DLL_PATH"], false, session) ? ActionResult.Success : ActionResult.Failure;
}
catch (Exception ex)
{
session.Log($"Error: {ex.Message}");
return ActionResult.Failure;
}
}
private static bool InvokeDll(string dllPath, bool register, Session session)
{
try
{
if (!Environment.Is64BitProcess)
{
throw new NotSupportedException("This action can only be run in a 64-bit process");
}
session.Log($"{nameof(InvokeDll)} {(register ? "register" : "unregister")} on DLL at path: {dllPath}");
if (!System.IO.File.Exists(dllPath))
{
session.Log($"Error: DLL not found at path: {dllPath}");
return false;
}
else
{
session.Log($"DLL found at path: {dllPath}");
}
var module = LoadLibrary(dllPath);
if (module == IntPtr.Zero)
{
int errorCode = Marshal.GetLastWin32Error();
session.Log($"Error: LoadLibrary failed with error code {errorCode}");
return false;
}
var procAddress = register
? GetProcAddress(module, "DllRegisterServer")
: GetProcAddress(module, "DllUnregisterServer");
if (procAddress == IntPtr.Zero)
{
session.Log("Error: can't find function in DLL");
return false;
}
var func = (DllRegisterServerDelegate)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(DllRegisterServerDelegate));
var hr = func();
if (hr != 0)
{
session.Log($"Error: operation failed (0x{hr:X8})");
return false;
}
session.Log($"Hyper Virtual Webcam has been successfully {(register ? "registered" : "unregistered")} to the system");
return true;
}
catch (Exception ex)
{
session.Log($"Error: {ex.Message}");
return false;
}
}
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadLibrary(string lpFileName);
private delegate int DllRegisterServerDelegate();
} here's my installation log:
|
Beta Was this translation helpful? Give feedback.
Answered by
StephenHodgson
Mar 14, 2024
Replies: 1 comment
-
Figured out my problem. My solution build configurations were somehow corrupted. I had to remove |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
StephenHodgson
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Figured out my problem. My solution build configurations were somehow corrupted. I had to remove
Any CPU
andx86
for bothRelease
andDebug