dllimport

Fill in DLL import table manually: IMAGE_IMPORT_DESCRIPTOR's Name field stores 0x0000FFFF

和自甴很熟 提交于 2019-11-30 10:01:26
问题 My goal is to fill in Dll's import table manually in order to hook internal LoadLibrary calls (when you load library it may load another library inside its DllMain). Here is my code which fill in Import table recursively for each dll in dependecies hierarchy and it works fine except some dlls ( api-ms-win-crt-locale-l1-1-0.dll in this case). void PEUtility::fillImportTable(HMODULE loadedModule, FillImportFlag flag, std::function<void(HMODULE&)> callback) { std::stack<HMODULE> modules; modules

PYTHON - Ctypes : OSError: exception: access violation writing 0xFFFFFFFFFA1C001B

笑着哭i 提交于 2019-11-30 09:40:27
问题 Here is a code for writing values to memory using memory mapping. When I try to run the code, I get the error "File "MMF.py", line 26, in memcpy(pBuf, szMsg, len(szMsg)) OSError: exception: access violation writing 0xFFFFFFFFFA1C001B" import msvcrt, mmap import ctypes from ctypes import * FILE_MAP_ALL_ACCESS = 0x04 INVALID_HANDLE_VALUE = 0xFFFFFFFF SHMEMSIZE = 256 PAGE_READWRITE = 0x04 szName = ctypes.c_wchar_p("MyFileMappingObject") szMsg = "Message from Python(ctypes) process" hMapObject =

How to set up a C++ function so that it can be used by p/invoke?

不问归期 提交于 2019-11-30 07:16:59
Hopefully this is a brainlessly easy question, but it shows my lack of expertise with C++. I'm a C# programmer, and I've done extensive work with P/Invoke in the past with other people's C++/C dlls. However, this time I've decided to write a wrapper C++ dll (unmanaged) myself, and am then calling my wrapper dll from C#. The problem I am immediately running into is that I am unable to define a C++ function that can be found by p/invoke. I don't know what the syntax for this is, but here's what I'm trying so far: extern bool __cdecl TestFunc() { return true; } Originally I simply had this, but

Setting dllimport programmatically in C#

半世苍凉 提交于 2019-11-30 07:12:25
I am using DllImport in my solution. My problem is that I have two versions of the same DLL one built for 32 bit and another for 64 bit. They both expose the same functions with identical names and identical signatures. My problem is that I have to use two static methods which expose these and then at run time use IntPtr size to determine the correct one to invoke. private static class Ccf_32 { [DllImport(myDllName32)] public static extern int func1(); } private static class Ccf_64 { [DllImport(myDllName64)] public static extern int func1(); } I have to do this because myDllName32 and

Parameterising DllImport for use in a C# application

核能气质少年 提交于 2019-11-30 06:55:56
We have a supplier who provides a library for access to their hardware. Unfortunately, if you have multiple devices, you need to import their library multiple times, with different dll names. As a consequence, we have a metric ton of duplicated code, and I'm worried that it will soon become be a maintenance nightmare. What we have at the moment is somthing like: namespace MyNamespace { public static class Device01 { public const string DLL_NAME = @"Device01.dll"; [DllImport(DLL_NAME, EntryPoint = "_function1")] public static extern int Function1(byte[] param); ... [DllImport(DLL_NAME,

DllImport generates System.DllNotFoundException

本秂侑毒 提交于 2019-11-30 05:42:27
问题 I’m having some difficulties while trying to consume an unmanaged-code dll from my application (written in C# framework 4.0). I’m using the dll import as follows [DllImport(@"C:\MGW_SDK.dll", EntryPoint = "fInicializaSDK")] public static extern int fInicializaSDK(); The weird thing is that when called from my development environment (Windows XP) it works just fine, but when on the production server (Windows7) it generates the following exception: System.DllNotFoundException : Unable to load

call unmanaged C++ code from C# using pinvoke

混江龙づ霸主 提交于 2019-11-30 05:16:20
问题 I have a unmanaged C++ dll for which I do not have access to code but have all methods declarations for. Lets for simplicity say that .h looks like this: #include <iostream> #ifndef NUMERIC_LIBRARY #define NUMERIC_LIBRARY class Numeric { public: Numeric(); int Add(int a, int b); ~Numeric(); }; #endif and method implementation in .cpp file int Numeric::Add(int a, int b) { return (a + b); } I simply want to call the add function from C++ in my C# code: namespace UnmanagedTester { class Program

DllNotFoundException with HRESULT 0x8007007E when loading 64-bit dll

六月ゝ 毕业季﹏ 提交于 2019-11-30 04:47:57
问题 I downloaded zlib and compiled the library as both Windows 32-bit and Windows 64-bit dll. I now have zlibwapi.dll and zlibwapi64.dll . The dlls are copied into my application folder and are referenced as follows: [DllImport(@"zlibwapi.dll", EntryPoint = "uncompress", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = false)] private static extern int uncompress32( IntPtr dest, ref uint destLen, [In(), MarshalAs(UnmanagedType.LPArray)]

Fill in DLL import table manually: IMAGE_IMPORT_DESCRIPTOR's Name field stores 0x0000FFFF

a 夏天 提交于 2019-11-29 18:17:25
My goal is to fill in Dll's import table manually in order to hook internal LoadLibrary calls (when you load library it may load another library inside its DllMain). Here is my code which fill in Import table recursively for each dll in dependecies hierarchy and it works fine except some dlls ( api-ms-win-crt-locale-l1-1-0.dll in this case). void PEUtility::fillImportTable(HMODULE loadedModule, FillImportFlag flag, std::function<void(HMODULE&)> callback) { std::stack<HMODULE> modules; modules.push(loadedModule); while (modules.size()) { auto module = modules.top(); modules.pop(); auto

Why use DllImport Attribute as apposed to adding a reference?

杀马特。学长 韩版系。学妹 提交于 2019-11-29 17:57:53
问题 I've seen a couple of examples such as this: [DllImport("user32.dll")] static extern bool TranslateMessage([In] ref Message lpMsg); [DllImport("user32.dll")] static extern IntPtr DispatchMessage([In] ref Message lpmsg); But, what I don't understand is why someone would do that as apposed to just referencing the DLL like they do other libraries? The MSDN states: "The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. For instance, your managed