DLL不能在C#中工作

时间:2015-12-01 12:41:15

标签: c# c++ .net dll

每当我尝试通过Visual Studio在C#中引用我自己的DLL时,它告诉我它无法引用DLL,因为它不是COM库。

我已经在互联网上搜索了一个解决方案,没有明确的答案或任何真正的帮助。这是一个相当简单的" DLL从指纹扫描仪捕获原始图像数据。我已经测试过,在我尝试将它变成DLL之前,C ++代码工作正常,只是你知道。

我遵循微软指南如何制作DLL,这就是我最终的目标:

  • JTBioCaptureFuncsDll.h
  • JTBioCaptureFuncsDll.cpp
  • JTBioCapture.cpp

JTBioCaptureFuncsDll.h

#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;
};

JTBioCaptureFuncsDll.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;
}

这个想法是在C#中你打电话给#34; CaptureSample()&#34;然后代码尝试捕获指纹扫描。当它进行扫描时,应该将结构返回到它可以使用的C#,保持:

  • 字节缓冲区
  • 图像高度
  • 图片宽度
  • Image Pixeldepth

但是当我尝试在我的C#项目中引用DLL时,我收到以下错误:

Error

我还尝试使用TlbImp.exe工具制作DLL但无济于事。它告诉我DLL不是有效的类型库。

所以我在这里有点失落。我是C ++的新手,所以制作一个Interop / COM组件不是我以前做过的,也不是在C#中使用DLL。

1 个答案:

答案 0 :(得分:1)

您无法在.NET项目中引用用C ++编写的非托管代码库。 因此,要从此类库调用代码,您必须使用DllImport,或使用WrapperClass

我提到了这个答案:https://stackoverflow.com/a/574810/4546874