C# 调用C++ dll 指向指针的指针 对应数据类型

dll 方法:


           int ADL_Display_DisplayMapConfig_Get (int iAdapterIndex, int *lpNumDisplayMap,  **lppDisplayMap, int *lpNumDisplayTarget,  **lppDisplayTarget, int iOptions);


C#调用:

            [DllImport("abc.dll")]

            public static extern int  ADL_Display_DisplayMapConfig_Get(int iAdapterIndex, ref int lpNumDisplayMap, out IntPtr lppDisplayMap, ref int lpNumDisplayTarget, out IntPtr lppDisplayTarget, int iOptions);


调用后读取返回值:

            List<ADLDisplayMap> mapList = new List<ADLDisplayMap>();

            ADLDisplayMap map = new ADLDisplayMap();

            for (int i = 0; i < iNumDisplayMap; i++)

            {

                map = (ADLDisplayMap)Marshal.PtrToStructure(new IntPtr(pDisplayMap.ToInt32() + i * Marshal.SizeOf(map)), map.GetType());

                mapList.Add(map);

            }


            List<ADLDisplayTarget> targetList = new List<ADLDisplayTarget>();

            ADLDisplayTarget target = new ADLDisplayTarget();

            for (int i = 0; i < iNumDisplayTarget; i++)

            {

                target = (ADLDisplayTarget)Marshal.PtrToStructure(new IntPtr(pDisplayTarget.ToInt32() + i * Marshal.SizeOf(target)), target.GetType());

                targetList.Add(target);

            }


指针普通应用情况 

            ADLMode mode = new ADLMode();

            IntPtr pTransientModes = IntPtr.Zero;

            pTransientModes = Marshal.AllocCoTaskMem(Marshal.SizeOf(mode));

            Marshal.StructureToPtr(mode, pTransientModes, false);


            mode= (ADLMode)Marshal.PtrToStructure(pTransientModes, mode.GetType());


获取数组的指针

           ADLSLSOffset[] appliedBezelOffsets = new ADLSLSOffset[n];

           lpAppliedBezelOffsets = Marshal.UnsafeAddrOfPinnedArrayElement(appliedBezelOffsets, 0); 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在C#调用C++ DLL并将指针指向的图像显示出来,您可以使用以下步骤: 1. 在C++ DLL中,创建一个函数来获取图像的指针并返回它。 例如: ```c++ extern "C" __declspec(dllexport) void* GetImagePointer() { // Your code to obtain the image pointer goes here // For example, if you have a pointer to the image data: // void* imageDataPointer = <your code to get the pointer>; return imageDataPointer; } ``` 2. 在C#中,使用DllImport属性来引入C++ DLL中的函数,并声明它的返回类型为IntPtr,这是一个指向未指定类型的指针。 例如: ```c# [DllImport("YourCppDll.dll")] public static extern IntPtr GetImagePointer(); ``` 3. 在C#中,将IntPtr转换为指向图像数据指针类型。例如,如果您的图像数据是一维的char数组,您可以使用Marshal.Copy方法将IntPtr转换为byte[],然后在显示图像时将其转换为Bitmap。 例如: ```c# // Call the C++ DLL function to get the image pointer IntPtr imagePointer = GetImagePointer(); // Convert the IntPtr to a byte array byte[] imageData = new byte[width*height]; Marshal.Copy(imagePointer, imageData, 0, imageData.Length); // Create a Bitmap from the byte array Bitmap image = new Bitmap(width, height, PixelFormat.Format8bppIndexed); ColorPalette palette = image.Palette; for (int i = 0; i < palette.Entries.Length; i++) { palette.Entries[i] = Color.FromArgb(i, i, i); } image.Palette = palette; BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); Marshal.Copy(imageData, 0, bitmapData.Scan0, imageData.Length); image.UnlockBits(bitmapData); // Display the image pictureBox.Image = image; ``` 请注意,这只是一个示例代码,需要根据您的要求进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_老杨_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值