我在visual studio 2013中创建了一个DLL,它构建得很好。它是一个Visual C ++项目,但用C语言编写。但是,我无法添加我构建的.dll作为我的C#项目的引用。我明白了:
A reference to 'C:\...\FileGuidUtils.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
我在线跟踪了一堆不同的教程,结果相同。 The only MSDN tutorial我发现不考虑C代码,只考虑C ++。
这是我的FileGuidUtils.cpp文件:
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <ole2.h>
#include <errno.h>
extern "C" __declspec(dllexport) WCHAR* getFileIdFromPath(_TCHAR* path);
extern "C" __declspec(dllexport) WCHAR* getPathFromFileId(_TCHAR* guid);
__declspec(dllexport) WCHAR* getFileIdFromPath(_TCHAR* path) {
HANDLE hFile;
FILE_OBJECTID_BUFFER buf;
DWORD cbOut;
GUID guid;
WCHAR *szGuid = (WCHAR *)malloc(sizeof(WCHAR) * 39);
BOOL result;
hFile = ::CreateFile(path, 0,
FILE_SHARE_READ |
FILE_SHARE_WRITE |
FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
fprintf(stderr, "CreateFile() Error: 0x%0.8x\n", ::GetLastError());
::CloseHandle(hFile);
return NULL;
}
result = ::DeviceIoControl(hFile,
FSCTL_CREATE_OR_GET_OBJECT_ID,
NULL, 0,
&buf, sizeof(buf),
&cbOut, NULL);
if (!result) {
fprintf(stderr, "DeviceIoControl() Error: 0x%0.8x\n", ::GetLastError());
::CloseHandle(hFile);
return NULL;
}
::CopyMemory(&guid, &buf.ObjectId, sizeof(GUID));
::StringFromGUID2(guid, szGuid, 39);
return szGuid;
}
__declspec(dllexport) WCHAR* getPathFromFileId(_TCHAR* guid) {
struct FILE_NAME_INFO_AND_BUF {
FILE_NAME_INFO fni;
TCHAR buf[260];
};
HANDLE hRoot;
FILE_ID_DESCRIPTOR desc;
HANDLE hFile;
FILE_NAME_INFO_AND_BUF fnib = { 0 };
HRESULT hr;
TCHAR szRoot[] = _T("C:");
BOOL result;
hRoot = ::CreateFile(szRoot, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hRoot == INVALID_HANDLE_VALUE) {
_tprintf(_T("CreateFile() Error: 0x%0.8x\n"),
::GetLastError());
return NULL;
}
desc.dwSize = sizeof(desc);
desc.Type = ObjectIdType;
hr = ::CLSIDFromString(guid, &desc.ObjectId);
if (FAILED(hr)) {
fprintf(stderr, "CLSIDFromString() Error: 0x%0.8x\n", ::GetLastError());
::CloseHandle(hRoot);
return NULL;
}
hFile = ::OpenFileById(hRoot, &desc, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 0);
if (hFile == INVALID_HANDLE_VALUE) {
fprintf(stderr, "OpenFileById() Error: 0x%0.8x\n", ::GetLastError());
::CloseHandle(hFile);
return NULL;
}
result = ::GetFileInformationByHandleEx(hFile, FileNameInfo, &fnib, sizeof(fnib));
if (!result) {
fprintf(stderr, "GetFileInformationByHandleEx Error %d\n", ::GetLastError());
::CloseHandle(hFile);
return NULL;
}
wchar_t *path = (wchar_t *)malloc(sizeof(wchar_t) * 512);
wcscpy_s(path, 256, szRoot);
wcscat_s(path, 256, fnib.fni.FileName);
return path;
}
还有其他自动生成的文件,但我没有修改它们,除了在stdafx.h中注释掉#define WIN32_LEAN_AND_MEAN
行,因为我的项目需要完整版本的Windows.h。
答案 0 :(得分:2)
并且它是有效的程序集或COM组件
它既不是。您必须在C#项目中编写[DllImport]
声明才能使用它。
如果您希望Add Reference工作,那么最简单的方法是使用public ref class
创建C ++ / CLI项目。文件&gt;添加&gt;新项目&gt; Visual C ++&gt; CLR&gt;类库模板。需要最少的C ++ / CLI语法知识才能使其达到良好的目的。