【发布时间】:2017-09-11 09:44:00
【问题描述】:
我正在用 C++ 编写一个 DLL 来搜索位图中的子图片。
执行 C++ 部分时,HBITMAP 无效。
这是我的代码:
C#:
[System.Runtime.InteropServices.DllImport("FindSubPicture.dll", EntryPoint = "FindSubPictures", CallingConvention = CallingConvention.Cdecl)]
private static extern System.IntPtr FindSubPicturesFct(System.IntPtr mImg, System.IntPtr sImg, int* nMatches);
public static System.Collections.Generic.List<TPoint> FindSubPictures(System.Drawing.Bitmap mImg, System.Drawing.Bitmap sImg)
{
TPoint* PStack = null;
int nMatches = 0;
System.Collections.Generic.List<TPoint> MyList = new System.Collections.Generic.List<TPoint>();
MyList.Clear();
PStack = (TPoint*)FindSubPicturesFct(mImg.GetHbitmap(), sImg.GetHbitmap(), &nMatches);
if(PStack == null) { return MyList;}
for (int i = 0; i < nMatches; i++) { MyList.Add(new TPoint(PStack[i].x[0], PStack[i].x[1])); }
try
{
System.Runtime.InteropServices.Marshal.FreeHGlobal((System.IntPtr)PStack);
}catch(System.Exception ex) { System.Console.WriteLine(ex.Message); }
return MyList;
}
C++:
#include "FindSubPictures.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <omp.h>
struct TPoint
{
int x[2];
TPoint(int fx, int fy) {
x[0] = fx;
x[1] = fy;
}
void Reset() { x[0] = 0; x[1] = 0; }
void Set(int fx, int fy) { x[0] = fx; x[1] = fy; }
};
extern "C" __declspec(dllexport) TPoint* FindSubPictures(HBITMAP mImg, HBITMAP sImg, int* nMatches) {
int mImgWidth = -1, mImgHeight = -1, sImgWidth = -1, sImgHeight = -1;
TPoint* MyList = nullptr;
if (mImg == nullptr || sImg == nullptr || nMatches == nullptr) { return nullptr; }
return MyList;
}
嗯,C++ 库函数直到现在还没有做任何事情。那是因为HBITMAP 无效。在 C# 中,System.Drawing.Bitmap 是有效的。
当我输入“mIng.”时,没有自动完成功能。
我错过了什么吗?
【问题讨论】:
-
参数列表中的匹配数在哪里?
-
什么意思?
-
我的错误。 c# 中的调用函数缺少不需要的匹配项。 : 公共静态 System.Collections.Generic.List
FindSubPictures(System.Drawing.Bitmap mImg, System.Drawing.Bitmap sImg)
标签: c# c++ bitmap interop hbitmap