I created a DLL in visual studio 2013, and it built fine. It is a Visual C++ project, but written in C. However, I'm unable to add the .dll I built as a reference to my C# project. I get:
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.
I followed a bunch of different tutorials online and same result. The only MSDN tutorial I found doesn't account for C code, only C++.
Here is my FileGuidUtils.cpp file:
#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;
}
There were other autogenerated files but I did not modify them, save for commenting out the #define WIN32_LEAN_AND_MEAN
line in stdafx.h since my project needs the full version of Windows.h.
and that it is a valid assembly or COM component
It is neither. You'll have to write [DllImport]
declarations in your C# project to use it.
If you want Add Reference to work then the simplest way is to create a C++/CLI project with a public ref class
. File > Add > New Project > Visual C++ > CLR > Class Library template. Minimal knowledge of C++/CLI syntax is required to bring it to a good end.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.