我在CLR控制檯應用程序中測試了一個使用C++/CLI編寫的簡單DLL。該DLL只有一個功能,我正在嘗試使用。我正在引用DLL並在項目屬性頁面中設置Resolve #using Reference,但是我看不到我寫的函數。我猜測我可能錯過了某處的訪問修飾符,但我不確定。這裏是我的代碼崩潰:在測試應用程序中看不到我的DLL函數
DLL代碼頭:
// LogDLL.h
#pragma once
#using <mscorlib.dll>
using namespace System;
namespace LogDLL {
public ref class LogFuncs
{
// TODO: Add your methods for this class here.
LogFuncs(){;};
~LogFuncs(){;};
void log_to_file (System::String ^file, bool overwrite, System::String ^text);
};
}
DLL代碼來源:
#include "stdafx.h"
#include "LogDLL.h"
using namespace System::Globalization;
void LogDLL::LogFuncs::log_to_file (System::String ^file, bool overwrite, System::String ^text)
{
//Do Stuff
}
而且我使用的測試代碼:
#include "stdafx.h"
#using <LogDLL.dll>
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
LogDLL::LogFuncs^ a;
a::LogDLL::LogFuncs:: //<-- Intellisense doesn't show the function from the DLL
return 0;
}
再次,我不是蘇重新我失蹤了。我已經使用C++/CLI已經有一段時間了,所以我很生疏。
UPDATE:
我繼續改類每彼得的忠告結構。
修改DLL頭代碼:
// LogDLL.h
#pragma once
#using <mscorlib.dll>
using namespace System;
namespace LogDLL {
public ref struct LogFuncs
{
// TODO: Add your methods for this class here.
LogFuncs(){;};
~LogFuncs(){;};
void log_to_file (System::String ^file, bool overwrite, System::String ^text);
};
}
我仍然不明白的是爲什麼這個類仍然會默認爲私有,即使我將它指定爲公用。有這樣的根本原因嗎?如果我使用非託管C++,會有什麼不同嗎?
感謝彼得,我改變它爲ref結構,並做了伎倆。 – kingrichard2005 2011-02-25 21:20:44
對於您的後續問題,課堂上的「公共」本身是指類型的可見性,而不是其成員的類型。如果你只是「引用類LogFuncs」,它將不能在它自己的程序集之外訪問。因此,如果會員想以其他方式訪問會員,則需要使用公共類型(忽略InternalsVisibleToAttribute),並且該會員本身需要公開。 – 2011-02-25 21:28:29
額外的註釋可能會消除混淆:C++/CLI嘗試將C++儘可能多地集成到CLI(通用語言基礎結構)中的概念中。因此,在C++/CLI中有兩種聲明CLI類類型的方法,即「ref class」和「ref struct」。前者的行爲就像一個C++類:成員的默認私人訪問。後者的行爲就像一個C++結構:成員的默認公共訪問。對於「值類」和「值結構」也是如此,除了這些聲明CLI值類型。 – 2011-02-25 21:45:24