在C#中使用外部C声明 [英] Using External C declarations in C#
问题描述
我对要在C#应用程序中调用的dll中的函数有一个外部声明.
声明的形式为
I have an external declaration for a function in a dll I wish to call in a C# application.
The declaration is in the form
extern int WINAPI PassBuffer(void *lpDib);
其中* lpDib表示要传递给dll的位图或Dib.我在dll的".h"文件中找到了此文件.
我不确定实际发生了什么,但我认为lpDib是我要传递的bmp的指针.
我在C#应用程序中将函数声明为:
where *lpDib refers to a bitmap or Dib to be passed to the dll. This I found in a ''.h'' file for the dll.
I''m not certain whats actually happening but I assume, lpDib is a pointer to the bmp I want to pass.
I''ve declared the function in my C# application as:
private static extern int PassBuffer(IntPtr lpDib);
首先,这是正确的吗?其次如何获取指向我的bmp的指针.
其声明:
Firstly, is this correct? and secondly how do i get the pointer to my bmp.
Its declared:
Bitmap bmp = new Bitmap("c:\\My.JPG");
如何获取bmp的指针以
How do I get the pointer of the bmp to hand to
PassBuffer(whatgoeshere);
谢谢您的帮助,我知道这将是一个愚蠢的问题,答案很简单,但让我感到困惑...
Thanks for any help, I know its going to be a dumb question with a simple answer but its got me stumped...
推荐答案
尝试使用此方法:
Try using this:
// Create a new bitmap.
Bitmap bmp = new Bitmap("c:\\my.jpg");
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
然后在PassBuffer中使用ptr.
Then use the ptr in PassBuffer.
这篇关于在C#中使用外部C声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!