Tag: pinvoke

ReadProcessMemory和WriteProcessMemory错了

我正在创建这个内存编辑器,但子程序ReadProcessMemory失败; WriteProcessMemory也是如此。 我试图通过使用GetLastError子例程获取最后一个错误,但它返回0,即ERROR_SUCCESS。 这是程序和类的代码。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Memedit; using System.Runtime.InteropServices; namespace Memory_Editor { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop, false)) { e.Effect = DragDropEffects.All; } } […]

在C ++和C#之间传递vector struct

我有c ++非托管代码,我想从c#访问。 所以我按照了一些教程,为我的项目构建了一个dll(只有一个类btw)。 现在我想从c#中使用它,我正在使用p / invoke,如下所示。 我的问题是:是否有可能对我的Windows点进行编组,以便将其作为向量传递到我的c ++代码中? 我可以更改所有代码(除了qwindows点,但我可以自己指出)。 有没有我不需要创建交流包装的解决方案? 我正在关注这个问题: 如何使用st#:: vector :: iterator作为C#中的参数调用非托管C ++函数? 非常感谢ps,我发现了一个“解决方案”,但我无法查看它http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21461195.html C# using Point = System.Windows.Point; class CPlusPlusWrapper { [DllImport(“EmotionsDLL.dll”, EntryPoint = “calibrate_to_file”)] static extern int calibrate_to_file(vector pontos);//marshall here [DllImport(“EmotionsDLL.dll”, EntryPoint = “calibration_neutral”)] static extern int calibration_neutral(); /// /// wraps c++ project into c# /// public void calibrate_to_file() { […]

PInvoke和char **

我从我想在c#应用程序中使用的人那里得到了这个程序集。 标题看起来像这样: int __declspec(dllimport) s2o(WCHAR* filename, char** out, int* len); 我设法让它部分工作,使用: [DllImport(“s2o.dll”, EntryPoint = “?skn2obj@@YAHPA_WPAPADPAH@Z”, CallingConvention = CallingConvention.Cdecl)] public static extern int s2o( [MarshalAs(UnmanagedType.LPWStr)] string filename, ref char[] @out, ref int len ); 然后像这样调用它: char[] result = null; int length = 0; s2o(“filepath”, ref result, ref length); 它似乎部分起作用,因为“长度”实际上得到了一个值。 不幸的是,’结果’保持为空。 我应该怎么做才能使这个工作? 编辑: 好吧,我设法通过用IntPtr替换char []然后像Nick建议调用’Marshal.PtrToStringAnsi’来工作: string result […]

将C结构移植到C#

我将C代码移植到C#,我有些疑惑。 考虑这个结构: typedef struct { uint32_t w; uint32_t h; uint32_t f_cc; uint32_t st; unsigned char *pl[4]; int32_t strd[4]; void (*done)(void *thisobj); void *e_cc; uint32_t rsrvd2; uint32_t rsrvd3; } f_tt; 我已经完成了这个并且它不起作用(可能是因为它错了: – /): [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct f_tt { public uint w; public uint h; public uint f_cc; public uint st; [MarshalAs(UnmanagedType.ByValArray, SizeConst = […]

无法在Unity中编组从C ++到C#的结构数组

我正在尝试将C ++中的struct数组传递给C#中的Unity脚本。 当我在生产中使用代码时,数组的大小会有很大差异,所以我实际上需要传递一个未知长度的数组。 我的聪明想法是将数组存储在堆上,并将对该数组的引用传递给Unity。 我发现StackOverflow的post就是如何做到的。 但是然后Unity抱怨引用为null。 这是我的C ++代码的一部分: extern “C” struct S; // Not currently used. struct S { int i; float f; }; extern “C” bool helloWorld(S ** a, int * i); S * s; bool helloWorld(S ** a, int * i) { s = new S[4]; for (int j = 0; j < […]

从c#调用dll中的Delphi方法

我试图使用以下签名调用Delphi DLL中的方法: function SMap4Ovr(const OverFileName : ShortString ; const Aclay : Integer ; const Acarbon : Double ; out errstr : ShortString): WordBool; 我在C#中使用以下导入: [DllImport(“SMap.dll”, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern bool SMap4Ovr( string OverFileName, int Aclay, double Acarbon, out string errstr ); 但我得到一个AccessViolationException。 我似乎能够在DLL中调用几个更简单的方法,这些方法有字符串参数但不是整数或双精度数。 我也试过CallingConvention = CallingConvention.Cdecl,但这给了我同样的错误。

适应二维情况的包装类

这个问题是这个问题的延伸。 我想适应二维情况的包装。 这是我的第一次尝试: public class EmxArrayRealTWrapper : IDisposable { private readonly emxArray_real_T _value; private GCHandle _dataHandle; private GCHandle _sizeHandle; public emxArray_real_T Value { get { return _value; } } public EmxArrayRealTWrapper(double[,] data) { _dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned); _value.data = _dataHandle.AddrOfPinnedObject(); _sizeHandle = GCHandle.Alloc(new int[] { data.GetLength(0), data.GetLength(1) }, GCHandleType.Pinned); _value.size = _sizeHandle.AddrOfPinnedObject(); _value.allocatedSize = data.GetLength(0) […]

C#中的_BitScanForward?

我正在将用C ++编写的程序翻译成C#,并且我遇到了一个我无法解决的内在函数。 在C ++中,这被称为: unsigned char _BitScanForward(unsigned long * Index, unsigned long Mask); 如果我只知道内部函数所在的DLL(如果有的话),我可以使用P / Invoke。 由于我不知道,我在.NET框架中寻找替代方案,但我空手而归。 有谁知道如何在_BitScanForward上使用P / Invoke,或者做同样事情的.NET方法? 感谢任何帮助,谢谢。

C#委托中的Marshal va_list

我正在尝试从c#开始这项工作: C头: typedef void (LogFunc) (const char *format, va_list args); bool Init(uint32 version, LogFunc *log) C#实现: static class NativeMethods { [DllImport(“My.dll”, SetLastError = true)] internal static extern bool Init(uint version, LogFunc log); [UnmanagedFunctionPointer(CallingConvention.Cdecl, SetLastError = true)] internal delegate void LogFunc(string format, string[] args); } class Program { public static void Main(string[] args) { NativeMethods.Init(5, LogMessage); […]

如何使用C#调用接收Delphi开放数组参数的函数?

如何将Delphi代码转换为C#? 它需要一个array of Byte ,但我不确定C#的等价物是什么。 我的尝试不起作用,并抛出像AccessViolationException之类的exception。 delphi: function SetLevel(a: array of byte): boolean; stdcall; external ‘DMX510.dll’; C#: [DllImport(“DMX510.DLL”)] public static extern Boolean SetLevel(Byte[] bytearray); Byte[] byteArray = new Byte[5]; byteArray[1] = 75; SetLevel(byteArray);