I need to implement the following C++ code into C#.
PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY signaturePolicy = { };
signaturePolicy.MicrosoftSignedOnly = true;
SetProcessMitigationPolicy(ProcessSignaturePolicy, &signaturePolicy, sizeof(signaturePolicy));
I have already defined the extern and the struct, but i dont know to use the parameters for the function.
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetProcessMitigationPolicy(int policy, PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY lpBuffer, int size);
private struct PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY
{
private uint MicrosoftSignedOnly;
}
public static void Main(string[] args)
{
SetProcessMitigationPolicy(??) // what do do there? how to get the required sizeof?
}
Can anyone explain me how to pass the required parameters in the right way? Thanks.
Related
I am loading OpenGL in C# using LoadLibrary/GetProcAddress/wglGetProcAddress. For some reason, it fails to load the pointer for glBlendFunc, and other random ones after that point. I have been looking through my code for the better part of an hour now, and all these failing parts look completely fine and normal?
The code to load the function pointers and cast it to a delegate.
public override T GetFunction<T>(string name)
{
IntPtr pointer = wglGetProcAddress(name);
if (pointer == IntPtr.Zero)
pointer = GetProcAddress(Handle, name);
return Marshal.GetDelegateForFunctionPointer<T>(pointer);
}
A sample of the code that calls GetFunction.
BlendEquationSeparate = Loader.GetFunction<glBlendEquationSeparate>("glBlendEquationSeparate");
BlendEquationSeparatei = Loader.GetFunction<glBlendEquationSeparatei>("glBlendEquationSeparatei");
BlendFunc = Loader.GetFunction<glBlendFunc>("glBlendFunc");
BlendFunci = Loader.GetFunction<glBlendFunci>("glBlendFunci");
BlendFuncSeparate = Loader.GetFunction<glBlendFuncSeparate>("glBlendFuncSeparate");
BlendFuncSeparatei = Loader.GetFunction<glBlendFuncSeparatei>("glBlendFuncSeparatei");
A sample of the delegate code.
public delegate void glBlendEquationSeparate(BlendEquationMode modeRGB, BlendEquationMode modeAlpha);
public static glBlendEquationSeparate BlendEquationSeparate;
public delegate void glBlendEquationSeparatei(UInt32 buf, BlendEquationMode modeRGB, BlendEquationMode modeAlpha);
public static glBlendEquationSeparatei BlendEquationSeparatei;
public delegate void glBlendFunc(BlendingFactorSrc sfactor, BlendingFactorDest dfactor);
public static glBlendFunc BlendFunc;
public delegate void glBlendFunci(UInt32 buf, BlendingFactorSrc sfactor, BlendingFactorDest dfactor);
public static glBlendFunci BlendFunci;
public delegate void glBlendFuncSeparate(BlendingFactorSrc srcRGB, BlendingFactorDest dstRGB, BlendingFactorSrc srcAlpha, BlendingFactorDest dstAlpha);
public static glBlendFuncSeparate BlendFuncSeparate;
public delegate void glBlendFuncSeparatei(UInt32 buf, BlendingFactorSrc srcRGB, BlendingFactorDest dstRGB, BlendingFactorSrc srcAlpha, BlendingFactorDest dstAlpha);
public static glBlendFuncSeparatei BlendFuncSeparatei;
The DllImports I am using
[DllImport("kernel32")]
public static extern IntPtr LoadLibrary(string fileName);
[DllImport("kernel32")]
public static extern IntPtr GetProcAddress(IntPtr module, string procName);
[DllImport("opengl32.dll", EntryPoint = "wglGetProcAddress", ExactSpelling = true)]
public static extern IntPtr wglGetProcAddress(string lpszProc);
All the enums are pretty standard, and need no context. I load the library like usual: LoadLibrary(opengl32.dll);. Can anyone explain to me where I am going wrong?
I have found a somewhat strange solution. If I move the code to a static constructor so that it runs at the beginning of the program, it suddenly works. I have no idea why my first approach didn't work.
all!
Please, help me with any advice with my problem: I build GUI WinForms application and now I want to attach console to it. I found this is not as much easy as it seems before. But I found good solution here: How do I show a console output/window in a forms application? Below the code from rag answer.
using System;
using System.Runtime.InteropServices;
namespace SomeProject
{
class GuiRedirect
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(StandardHandle nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetStdHandle(StandardHandle nStdHandle, IntPtr handle);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern FileType GetFileType(IntPtr handle);
private enum StandardHandle : uint
{
Input = unchecked((uint)-10),
Output = unchecked((uint)-11),
Error = unchecked((uint)-12)
}
private enum FileType : uint
{
Unknown = 0x0000,
Disk = 0x0001,
Char = 0x0002,
Pipe = 0x0003
}
private static bool IsRedirected(IntPtr handle)
{
FileType fileType = GetFileType(handle);
return (fileType == FileType.Disk) || (fileType == FileType.Pipe);
}
public static void Redirect()
{
if (IsRedirected(GetStdHandle(StandardHandle.Output)))
{
var initialiseOut = Console.Out;
}
bool errorRedirected = IsRedirected(GetStdHandle(StandardHandle.Error));
if (errorRedirected)
{
var initialiseError = Console.Error;
}
AttachConsole(-1);
if (!errorRedirected)
SetStdHandle(StandardHandle.Error, GetStdHandle(StandardHandle.Output));
}
}
This code works as charm except one downside: non-latin letters outputs to console in strange encoding (but if redirected to file, they are in right encoding). I need to redirect both StdOut and StdErr, and if I change any part of the code it stops redirecting.
Thanks to all who share their wisdom with me in comments!
SetConsoleOutputCP was the answer.
Don't forget put this to other definitions of the class.
[DllImport("kernel32.dll")]
static extern bool SetConsoleOutputCP(uint wCodePageID);
And than add call of the SetConsoleOutputCP(desired codepage); to Redirect() method.
I am trying to load a Fortran dll dynamically and pass a string back from fortran to C#. Everything looks fine when inside the fortran code, but when returning to C# the value of the string is lost. Instead the initial value, set in C#, is back. I have tried to use the 'ref' keyword to get the string to be passed by reference, but then I get error like below. What am I doing wrong?
The runtime has encountered a fatal error. The address of the error was at 0x709ce248, on thread 0x2ac4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
Fortran code:
module FortLibInterface
implicit none
integer, parameter :: STR_LENGTH = 256
contains
subroutine GetString(Str)
!DIR$ ATTRIBUTES DLLEXPORT::GetString
!DIR$ ATTRIBUTES ALIAS: 'GetString' :: GetString
!DIR$ ATTRIBUTES REFERENCE:: Str
character(len=STR_LENGTH), intent(inout) :: Str
Str = 'bcdef...'
end subroutine
end module
C# code:
using System;
using System.Runtime.InteropServices;
namespace FortranCSTest
{
class Program
{
static void Main(string[] args)
{
string dllPath = "C:\\Temp\\FortLib.dll";
FortLibTest lib = new FortLibTest(dllPath);
lib.MakeTestCall();
}
}
public class FortLibTest
{
public const int STR_LENGTH = 256;
public const string FortranFuncName = "GetString";
private string pathToDll = null;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary(String DllName);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
public FortLibTest(string FullPathToDll)
{
pathToDll = FullPathToDll;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void TypeGetStrInfo(char[] str);
void GetStrInfo(char[] str)
{
IntPtr pDll = LoadLibrary(pathToDll);
if (pDll != IntPtr.Zero)
{
IntPtr pFunc = GetProcAddress(pDll, FortranFuncName);
if (pFunc != IntPtr.Zero)
{
TypeGetStrInfo func = (TypeGetStrInfo)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(TypeGetStrInfo));
func(str);
}
else
{
//Something
}
FreeLibrary(pDll);
}
else
{
//Something
}
}
public void MakeTestCall()
{
char[] str = new char[STR_LENGTH];
str[0] = 'a';
GetStrInfo(str);
}
}
}
For future reference. I added [In, Out] and everything works.
[DllImport(_dllName, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void GetString([In, Out] char[] str);
I have a C++ assembly that I am importing using DLLImport.
I am attempting to call its method:
namespace Testing
{
class Test{
int Run(char* filePath, bool bEntry, double duration){//code}
};
}
by
[DllImport(dllName, CharSet = CharSet.Auto)]
public static extern int Run(string filePath, bool bEntry, double duration)
);
When I call its method, I get the error message:
Unable to find an entry point named Run in dll
The "Run" looks to be a non-static class method. Although, it's possible to call such methods from C# this is not the primary use-case. It would be way easier to consume it from .NET if you expose it via COM, or at-least as a plain C interface:
extern "C" __declspec(dllexport) void* Testing_Test_Create();
extern "C" __declspec(dllexport) void Testing_Test_Destroy(void* self);
extern "C" __declspec(dllexport) int Testing_Test_Run(void* self, char* filePath, bool bEntry, double duration);
And here is a sample how to call C++ class methods from C#:
// Test.cpp in NativeLib.dll
namespace Testing
{
class __declspec(dllexport) Test
{
public:
explicit Test(int data)
: data(data)
{
}
int Run(char const * path)
{
return this->data + strlen(path);
}
private:
int data;
};
}
// Program.cs in CSharpClient.exe
class Program
{
[DllImport(
"NativeLib.dll",
EntryPoint = "??0Test#Testing##QAE#H#Z",
CallingConvention = CallingConvention.ThisCall,
CharSet = CharSet.Ansi)]
public static extern void TestingTestCtor(IntPtr self, int data);
[DllImport(
"NativeLib.dll",
EntryPoint = "?Run#Test#Testing##QAEHPBD#Z",
CallingConvention = CallingConvention.ThisCall,
CharSet = CharSet.Ansi)]
public static extern int TestingTestRun(IntPtr self, string path);
static void Main(string[] args)
{
var test = Marshal.AllocCoTaskMem(4);
TestingTestCtor(test, 10);
var result = TestingTestRun(test, "path");
Console.WriteLine(result);
Marshal.FreeCoTaskMem(test);
}
}
Entry point names might be different for your build configuration/compiler, so use dumpbin utility to obtain them. Again, this is just a proof of concept, in real code it would be better to use COM.
See here: http://dotnetperls.com/dllimport
I'm not sure this will help if the function is a member of a class, but to locate the entry point by name, not ordinal, you'll need a .def file in your dll..
LIBRARY mylib
Run #1
C# static constructor and GetVersion() any suggestions?
Hi,
I have defined struct like this in separate file OSVERSIONINFO.cs like this:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OSVERSIONINFO
{
public static int SizeOf
{
get
{
return Marshal.SizeOf (typeof(OSVERSIONINFO));
}
}
public uint dwOSVersionInfoSize;
public uint dwMajorVersion;
public uint dwMinorVersion;
public uint dwBuildNumber;
public uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
}
Also I have this file OS.cs in which I have defined the following class:
public static class OS
{
static OS ()
{
OSVERSIONINFO info = new OSVERSIONINFO();
info.dwOSVersionInfoSize = (uint)OSVERSIONINFO.SizeOf;
if (!OS.GetVersion(ref info))
{
Console.WriteLine("Error!!!");
}
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetVersion (ref OSVERSIONINFO lpVersionInfo);
}
Way in static constructor of OS class population of info (instance of OSVERSIONINFO struct) fails?
If I call OS.GetVersion in other palce (not OS class) every thing is OK?
You should use the Environment.OSVersion.Platform property instead.
To answer the question, you need to call GetVersionEx.