Tag: 编组

C#从COM对象获取progID

我想知道是否有办法在c#中获取com对象的progId。 例如 – 我有一个webBrowser对象,它暴露了一个COM文档对象。 有没有办法弄清楚该文档对象的progID是什么? 我知道你可以从progID获取对象,只是不知道如何做反过来。

将托管代码中的多维数组传递给非托管代码

我想做以下事情: 在c#代码中创建三个像素数组,如下所示: var myArray = new short[x,y,z]; UnanagedFunction(myArray); 将它传递给非托管代码(c ++),如下所示: void UnmanagedFunction(short*** myArray) { short first = myArray[0][0][0]; } 更新当我尝试以下代码时,我遇到运行时错误: 尝试读取或写入受保护的内存。 谢谢!!!

如何在C#中编组结构数组?

我在C#中有以下结构: [StructLayoutAttribute(LayoutKind.Sequential)] public struct RECORD { public uint m1; public uint m2; public uint m3; } 我还需要将这些结构的数组 (固定长度)传递给本机代码,后者将一些数据写入这些结构。 该数组在C#中分配并传递给C dll。 我将导入的函数声明为: [DllImport(“marshall.dll”, CallingConvention = CallingConvention.Cdecl)] private static extern void doIt(RECORD[] dataRecord); 但我没有得到任何数据。 我已经尝试了PInvoke Interop助手。 我应该在这里使用IntPtr吗? 有任何想法吗? 编辑: 以下是调用本机函数的C#代码: RECORD[] rec = new RECORD[256]; doIt(rec); // values of rec are all zero here 这是C函数: int doIt(RECORD* […]

将结构数组转换为IntPtr

我试图将RECT结构的数组(如下所示)转换为IntPtr,因此我可以使用PostMessage将指针发送到另一个应用程序。 [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; // lots of functions snipped here } // so we have something to send, in reality I have real data here // also, the length of the array is not constant RECT[] foo = new RECT[4]; IntPtr […]

C#P / Invoke:包含函数指针的编组结构

对不起,下面是详细的介绍。 我需要知道P / Invoke内部人员的洞察力比我更好。 这是我如何编组包含从C到C#的函数指针的结构。 我想知道这是否是最干净和/或最有效的方式。 我正在使用C编码的本机DLL连接,它提供以下入口点: void* getInterface(int id); 您必须传递getInterface(int)以下枚举值之一: enum INTERFACES { FOO, BAR }; 它返回一个指向包含函数指针的结构的指针,如: typedef struct IFOO { void (*method1)(void* self, int a, float b); void (*method2)(void* self, int a, float b, int c); } IFoo; 以下是您在C中使用它的方式: IFoo* interface = (IFoo*)getInterface(FOO); interface->method1(obj, 0, 1.0f); // where obj is an instance of […]

C#中的等效char *

我有一个用c ++编写的dll。 我正在调用函数调用。 我有这个c ++声明。 int dll_registerAccount(char* username, char* password); 我做了这个dllimport声明: [DllImport(“pjsipDlld”)] static extern int dll_registerAccount(IntPtr username, IntPtr password); 我的DllImport是否相当于使用IntPtr的c ++? 非常感谢任何建议,

MessageBox.Show()是否自动编组到UI线程?

我通过ThreadPool.QueueUserWorkItem启动一个线程,其中有一个消息框对话框: System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show(“你想在后台下载升级吗?..”,“升级可用”,MessageBoxButtons.YesNo); 它看起来工作正常,但是在一些客户建议他们没有收到消息后我有点怀疑。 我有.NET Framework 2.0+的感觉你不需要编组这个特定的调用,它会为你做。 正确? 这是一个感兴趣的半相关主题: 为什么在MessageBox.Show中使用所有者窗口?

Marshal.PtrToStructure(以及返回)和字节顺序交换的通用解决方案

我有一个系统,远程代理发送序列化结构(来自嵌入式C系统)供我通过IP / UDP读取和存储。 在某些情况下,我需要发回相同的结构类型。 我以为我使用Marshal.PtrToStructure(接收)和Marshal.StructureToPtr(发送)进行了很好的设置。 但是,一个小问题是网络大端整数需要转换为我的x86小端格式才能在本地使用。 当我再次发送它们时,大端是可行的方式。 以下是有问题的function: private static T BytesToStruct(ref byte[] rawData) where T: struct { T result = default(T); GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned); try { IntPtr rawDataPtr = handle.AddrOfPinnedObject(); result = (T)Marshal.PtrToStructure(rawDataPtr, typeof(T)); } finally { handle.Free(); } return result; } private static byte[] StructToBytes(T data) where T: struct { byte[] […]

如何将包含可变大小数组的结构编组到C#?

我如何编组这个C ++类型? ABS_DATA结构用于将任意长的数据块与长度信息相关联。 Data数组的声明长度为1,但实际长度由Length成员给出。 typedef struct abs_data { ABS_DWORD Length; ABS_BYTE Data[ABS_VARLEN]; } ABS_DATA; 我尝试了以下代码,但它不起作用。 数据变量总是空的,我确定它有数据。 [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)] public struct abs_data { /// ABS_DWORD->unsigned int public uint Length; /// ABS_BYTE[1] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 1)] public string Data; }