我想在托管 C++ 项目中创建KeyValuePair的列表。这是我正在使用的语法
List<KeyValuePair<String^, String^>^>^ thing;
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
错误 C3225:“T”的泛型类型参数不能是“System::Collections::Generic::KeyValuePair ^”,它必须是值类型或引用类型的句柄
我基本上想这样做(C#)
List<KeyValuePair<string, string>> thing;
Run Code Online (Sandbox Code Playgroud)
但在托管 C++ 中。哦,在 .Net 2.0 中。有接受者吗?
在以下示例中,我得到:
error C2300: 'UnmanagedClass' : class does not have a finalizer called '!SmartPointer'
Run Code Online (Sandbox Code Playgroud)
如果我删除operator->,则此错误消失.有人可以解释为什么会这样吗?
// Unmanaged class.
class UnmanagedClass { };
public ref class SmartPointer {
public:
SmartPointer(UnmanagedClass* u) : m_ptr(u) { }
~SmartPointer() { this->!SmartPointer(); }
!SmartPointer() { delete m_ptr; }
// This line triggers C2300.
UnmanagedClass* operator->() { return m_ptr; }
};
int main() {
SmartPointer^ s = gcnew SmartPointer(new UnmanagedClass);
}
Run Code Online (Sandbox Code Playgroud) 我的c ++代码中有一堆int,我需要更改为int32.和我的布尔相同.为了使用int32和bool32,我需要包含哪个头.一旦我制作它们,我该如何声明它们.我能用int32替换int吗?
例如:
int x;
Run Code Online (Sandbox Code Playgroud)
变
int32 x;
Run Code Online (Sandbox Code Playgroud)
当我尝试从int更改为int32时,我遇到了很多错误.这里有几个:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2086: 'const int x' : redefinition
我的项目结构如下:
ASP.NET调用C#层调用托管 C++调用Native C++
(我试图避免使用互操作因此这就是托管c ++层的原因)
我编写单元测试来测试C#层并且它工作正常.当我尝试运行asp.net页面时,我得到:"Could not load file or assembly..."错误.我想通过当我将Native C++ dll 粘贴到"Temporary ASP.NET Files"(到相应的文件夹)时,该网站可以正常工作.
似乎Managed C++代码只有在它位于同一个文件夹中时才能找到Native C++代码 - 显然我不能在temp文件中拥有Native dll.
有没有办法在全局范围内设置Native(不适用于System32)?
谢谢你的评论.
我用代码设置服务器,它在cassini下运行,但是当我发布它(在iis7下运行)我得到"无法加载文件或程序集......"我正在运行IIS7 ApplicationPoolIdentity,.net 4集成非常感谢Pini.
我有一个通过委托调用的回调。在其中,我需要处理从记录过程到达的缓冲区数据。通常,在非托管上下文中,我可以对 dwParam1 执行reinterpret_cast 来获取对象引用。但在托管上下文中,如何将 DWORD_PTR 转换为托管对象引用?
static void WaveInProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
ControlLib::SoundDevice^ soudDevice = ?cast_native2managed?(dwParam1);
Run Code Online (Sandbox Code Playgroud) 我需要将下面的c#代码更改为c ++代码.
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
Run Code Online (Sandbox Code Playgroud)
在这个网站上,我找到了UTF8Encoding的c ++代码,我从中创建了这段代码
void StrToByteArray(string unicodeString)
{
UTF8Encoding^ utf8 = gcnew UTF8Encoding;
array<Byte>^encodedBytes = utf8->GetBytes( unicodeString );
}
Run Code Online (Sandbox Code Playgroud)
但这给了我以下错误
错误2错误C2664:'cli :: array ^ System :: Text :: Encoding :: GetBytes(cli :: array ^)':无法将参数1从'std :: string'转换为'cli :: array
为什么它会在与文档相同的情况下执行此操作?(除了我使用普通字符串,但使用顶级字符串^给我一个错误.)
我不确定它是否相关但我的代码是管理的.
注意:我还没有担心返回任何数据,直到我得到这个工作.
我在C++中有一个函数,它具有std :: string类型的值,并希望将其转换为String ^.
void(String ^outValue)
{
std::string str("Hello World");
outValue = str;
}
Run Code Online (Sandbox Code Playgroud) 嗨,我已经设计了一个控制台应用程序来测试我的C++项目(.Net framework 3.5).我为此应用程序创建了一个设置并将其安装在虚拟机上以进行测试.但每当我在VM上运行这个已安装的应用程序时,它就会给我一个未处理的异常,说:"无法加载XYZ程序集或其中一个依赖项.此应用程序无法启动,因为应用程序配置不正确.重新安装应用程序可能会解决问题(来自HRESULT的异常:0x800736B1)"
我怎样才能解决这个问题?
谢谢,卡皮尔
操作系统:xp
IDE:VS 2008
在我正在使用Visual C++进行的项目中,我已经声明了一个std::vector内部托管类
std::vector<pts> dataPoints;//this gives error c4368 : mixed type not allowed
Run Code Online (Sandbox Code Playgroud)
但这很有效
std::vector<pts> * dataPoints;//a pointer to the vector
Run Code Online (Sandbox Code Playgroud)
然后我在免费商店中创建了这个向量,就像在托管类的构造函数中一样
dataPoints = new std::vector<pts>(noOfElements,pts());//which is not so attractive.
Run Code Online (Sandbox Code Playgroud)
我需要矢量的原因是因为有文件我正在阅读ifstream并将这些值存储在矢量中.
Q1)为什么我能够声明指向本机类型的对象(我猜)而不是对象?此外,在尝试向量之前,我尝试了托管数组
cli::array<Point> dataPoints //and i defined it later.
Run Code Online (Sandbox Code Playgroud)
但是当我这样做的时候
ifile >> dataPoints[i].X;
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误c2678:operator =没有超载int!!
Q2)为什么我不能在这里使用托管代码.起初我认为它可能是一个包装类Int,但随后autounboxing(转换运算符)应该处理它?还是Point :: X是合格的property,因此不被认为是正常的int?我错过了什么?这就是我选择vector和pts 解决的原因.
pts如下
struct pts
{
int X, int Y;
pts() : X(0),Y(0){}
pts(int x,int y) : …Run Code Online (Sandbox Code Playgroud)