C# 韩元';我不能在C工作#
每当我试图通过VisualStudio在C#中引用自己的DLL时,它告诉我它无法引用DLL,因为它不是COM库 我在互联网上到处搜索,想找到一个解决方案,但没有明确的答案,也没有任何帮助。这是一个相当“简单”的DLL,它从指纹扫描仪捕获原始图片数据。在我尝试把它变成一个DLL之前,我已经测试过C++代码工作得很好。 我遵循Microsofts关于如何创建DLL的指南,以下是我的结论:C# 韩元';我不能在C工作#,c#,c++,.net,dll,C#,C++,.net,Dll,每当我试图通过VisualStudio在C#中引用自己的DLL时,它告诉我它无法引用DLL,因为它不是COM库 我在互联网上到处搜索,想找到一个解决方案,但没有明确的答案,也没有任何帮助。这是一个相当“简单”的DLL,它从指纹扫描仪捕获原始图片数据。在我尝试把它变成一个DLL之前,我已经测试过C++代码工作得很好。 我遵循Microsofts关于如何创建DLL的指南,以下是我的结论: jtbiocapturefuncsdl.h jtbiocapturefuncsdl.cpp JTBioCapt
- jtbiocapturefuncsdl.h
- jtbiocapturefuncsdl.cpp
- JTBioCapture.cpp
#ifdef JTBIOCAPTUREFUNCSDLL_EXPORTS
#define JTBIOCAPTUREFUNCSDLL_API __declspec(dllexport)
#else
#define JTBIOCAPTUREFUNCSDLL_API __declspec(dllimport)
#endif
using byte = unsigned char*;
struct BioCaptureSample {
INT32 Width;
INT32 Height;
INT32 PixelDepth;
byte Buffer;
};
jtbiocapturefuncsdl.cpp
// JTBioCapture.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
namespace JTBioCapture
{
using byte = unsigned char*;
class JTBioCapture
{
public:
// Returns a Struct with Information Regarding the Fingerprint Sample
static JTBIOCAPTUREFUNCSDLL_API BioCaptureSample CaptureSample();
};
}
JTBioCapture.cpp
/*
* Courtesy of WinBio God Satish Agrawal on Stackoverflow
*/
BioCaptureSample CaptureSample()
{
HRESULT hr = S_OK;
WINBIO_SESSION_HANDLE sessionHandle = NULL;
WINBIO_UNIT_ID unitId = 0;
WINBIO_REJECT_DETAIL rejectDetail = 0;
PWINBIO_BIR sample = NULL;
SIZE_T sampleSize = 0;
// Connect to the system pool.
hr = WinBioOpenSession(
WINBIO_TYPE_FINGERPRINT, // Service provider
WINBIO_POOL_SYSTEM, // Pool type
WINBIO_FLAG_RAW, // Access: Capture raw data
NULL, // Array of biometric unit IDs
0, // Count of biometric unit IDs
WINBIO_DB_DEFAULT, // Default database
&sessionHandle // [out] Session handle
);
if (FAILED(hr))
{
wprintf_s(L"\n WinBioOpenSession failed. hr = 0x%x\n", hr);
goto e_Exit;
}
// Capture a biometric sample.
wprintf_s(L"\n Calling WinBioCaptureSample - Swipe sensor...\n");
hr = WinBioCaptureSample(
sessionHandle,
WINBIO_NO_PURPOSE_AVAILABLE,
WINBIO_DATA_FLAG_RAW,
&unitId,
&sample,
&sampleSize,
&rejectDetail
);
if (FAILED(hr))
{
if (hr == WINBIO_E_BAD_CAPTURE)
{
wprintf_s(L"\n Bad capture; reason: %d\n", rejectDetail);
}
else
{
wprintf_s(L"\n WinBioCaptureSample failed. hr = 0x%x\n", hr);
}
goto e_Exit;
}
wprintf_s(L"\n Swipe processed - Unit ID: %d\n", unitId);
wprintf_s(L"\n Captured %d bytes.\n", sampleSize);
// Courtesy of Art "Messiah" Baker at Microsoft
PWINBIO_BIR_HEADER BirHeader = (PWINBIO_BIR_HEADER)(((PBYTE)sample) + sample->HeaderBlock.Offset);
PWINBIO_BDB_ANSI_381_HEADER AnsiBdbHeader = (PWINBIO_BDB_ANSI_381_HEADER)(((PBYTE)sample) + sample->StandardDataBlock.Offset);
PWINBIO_BDB_ANSI_381_RECORD AnsiBdbRecord = (PWINBIO_BDB_ANSI_381_RECORD)(((PBYTE)AnsiBdbHeader) + sizeof(WINBIO_BDB_ANSI_381_HEADER));
PBYTE firstPixel = (PBYTE)((PBYTE)AnsiBdbRecord) + sizeof(WINBIO_BDB_ANSI_381_RECORD);
int width = AnsiBdbRecord->HorizontalLineLength;
int height = AnsiBdbRecord->VerticalLineLength;
wprintf_s(L"\n ID: %d\n", AnsiBdbHeader->ProductId.Owner);
wprintf_s(L"\n Width: %d\n", AnsiBdbRecord->HorizontalLineLength);
wprintf_s(L"\n Height: %d\n", AnsiBdbRecord->VerticalLineLength);
BioCaptureSample returnSample;
byte byteBuffer;
for (int i = 0; i < AnsiBdbRecord->BlockLength; i++) {
byteBuffer[i] = firstPixel[i];
}
returnSample.Buffer = byteBuffer;
returnSample.Height = height;
returnSample.Width = width;
returnSample.PixelDepth = AnsiBdbHeader->PixelDepth;
/*
* NOTE: (width / 3) is necessary because we ask for a 24-bit BMP but is only provided
* a greyscale image which is 8-bit. So we have to cut the bytes by a factor of 3.
*/
// Commented out as we only need the Byte buffer. Comment it back in should you need to save a BMP of the fingerprint.
// bool b = SaveBMP(firstPixel, (width / 3), height, AnsiBdbRecord->BlockLength, L"C:\\Users\\smf\\Desktop\\fingerprint.bmp");
// wprintf_s(L"\n Success: %d\n", b);
e_Exit:
if (sample != NULL)
{
WinBioFree(sample);
sample = NULL;
}
if (sessionHandle != NULL)
{
WinBioCloseSession(sessionHandle);
sessionHandle = NULL;
}
wprintf_s(L"\n Press any key to exit...");
_getch();
return returnSample;
}
/*
*由WinBio God Satish Agrawal在Stackoverflow上提供
*/
生物捕获样本捕获样本()
{
HRESULT hr=S_正常;
WINBIO_SESSION_HANDLE sessionHandle=NULL;
WINBIO\u单元ID unitId=0;
WINBIO_拒绝_拒绝细节=0;
PWINBIO_BIR sample=NULL;
大小\u T样本大小=0;
//连接到系统池。
hr=WinBioOpenSession(
WINBIO_类型_指纹,//服务提供商
WINBIO\u池\u系统,//池类型
WINBIO_FLAG_RAW,//访问:捕获原始数据
NULL,//生物识别单元ID数组
0,//生物识别单元ID的计数
WINBIO_DB_DEFAULT,//默认数据库
&sessionHandle//[out]会话句柄
);
如果(失败(小时))
{
wprintf_s(L“\n WinBioOpenSession失败。hr=0x%x\n”,hr);
转到e_出口;
}
//捕获生物特征样本。
wprintf_s(L“\n调用WinBioCaptureSample-滑动传感器…\n”);
hr=WinBioCaptureSample(
会话句柄,
WINBIO_没有可用的目的,
WINBIO_数据_标志_原始,
&尤尼德,
&样本,
&抽样,
&拒绝细节
);
如果(失败(小时))
{
如果(hr==WINBIO\u E\u BAD\u捕获)
{
wprintf_s(L“\n错误捕获;原因:%d\n”,拒绝详细信息);
}
其他的
{
wprintf_s(L“\n WinBioCaptureSample失败。hr=0x%x\n”,hr);
}
转到e_出口;
}
wprintf_s(L“\n刷卡已处理-单元ID:%d\n”,单元ID);
wprintf_s(L“\n捕获了%d个字节。\n”,采样);
//由微软的艺术“弥赛亚”贝克提供
PWINBIO_BIR_HEADER BIR_HEADER=(PWINBIO_BIR_HEADER)((PBYTE)样本)+样本->HeaderBlock.Offset);
PWINBIO_BDB_ANSI_381_HEADER AnsiBdbHeader=(PWINBIO_BDB_ANSI_381_HEADER)((PBYTE)样本)+样本->标准数据块.Offset);
PWINBIO_BDB_ANSI_381_RECORD AnsiBdbRecord=(PWINBIO_BDB_ANSI_381_RECORD)((PBYTE)AnsiBdbHeader)+sizeof(WINBIO_BDB_ANSI_381_HEADER));
PBYTE firstPixel=(PBYTE)((PBYTE)AnsiBdbRecord)+sizeof(WINBIO_BDB_ANSI_381_RECORD);
int width=AnsiBdbRecord->HorizontalLineLength;
int height=AnsiBdbRecord->VerticalLineLength;
wprintf_s(L“\n ID:%d\n”,AnsiBdbHeader->ProductId.Owner);
wprintf_s(L“\n宽度:%d\n”,AnsibdRecord->HorizontalLineLength);
wprintf_s(L“\n高度:%d\n”,AnsiBdbRecord->VerticalLineLength);
生物捕获样品回收样品;
字节字节缓冲;
对于(int i=0;iBlockLength;i++){
byteBuffer[i]=第一像素[i];
}
returnSample.Buffer=byteBuffer;
返回样本。高度=高度;
返回样本。宽度=宽度;
returnSample.PixelDepth=AnsibdHeader->PixelDepth;
/*
*注:(宽度/3)是必要的,因为我们要求24位BMP,但仅提供
*一个8位的灰度图像。所以我们必须将字节数减少3倍。
*/
//注释掉,因为我们只需要字节缓冲区。如果需要保存指纹的BMP,请将其重新注释。
//bool b=SaveBMP(firstPixel,(宽度/3),高度,AnsiBdbRecord->BlockLength,L“C:\\Users\\smf\\Desktop\\fingerprint.bmp”);
//wprintf_s(L“\n成功:%d\n”,b);
紧急出口:
if(示例!=NULL)
{
生物树(样品);
样本=NULL;
}
if(sessionHandle!=NULL)
{
WinBioCloseSession(会话句柄);
sessionHandle=NULL;
}
wprintf_s(L“\n按任意键退出…”);
_getch();
退货样品;
}
其思想是在C#中调用“CaptureSample()”,然后代码尝试捕获指纹扫描。当它进行扫描时,应该将结构返回到它可以使用的C#,保持:
- 字节缓冲区
- 像高
- 图像宽度
- 图像像素深度
TlbImp.exe
工具创建DLL,但没有效果。它告诉我DLL不是有效的类型库
所以我有点迷路了。我是C++新手,所以在C++中使用一个互操作/ COM组件不是我以前做过的事情,也不是一个DLL。 < P>不能引用.NET项目中C++写的非托管代码库。 因此,要从这样的库中调用代码,您必须使用,或者使用
我提到了这个答案:。您需要使用DllImport。非托管(c++)dll不能直接添加到.net项目中。希望这个线程会帮助法尔罕,在非托管环境中,你需要使用DLimPurt。你需要使用P/Unjk,构建一个C++ /CLI包装器,或者将C++ DLL作为COM构建。只有最后两个可以添加dll作为引用。你让它工作了吗?如果使用(宽度/3),它将生成收缩图像。你解决这个问题了吗?请上传你的代码好的,谢谢你的信息!我会试试看:)