我编写了以下代码从指针函数返回多维数组.此函数的输入参数是一维数组,输出是指向多维数组的指针.
double **function( array< double>^ data,int width,int height ) {
int i;
double **R = new double *[height];
for (i=0;i<=height;i++)
R[i]=new double [width];
// ....
return R;
}
int main( void ) {
int M=2, N=10, i,j;
// define multimensional array 2x10
array< array< double >^ >^ input = gcnew array< array< double >^ >(M);
for (j=0; j<input->Length; j++) {
input[j]=gcnew array<double>(N);}
double **result1 = new double *[N];
for(i=0; i<=N; i++)
result1[i]=new double [M];
double **result2 = new double *[N]; …Run Code Online (Sandbox Code Playgroud) 执行对象比较时,按名称(字符串)或类型(指针)进行比较是否更快?
见下文:
if(sender is DataGridView) { .. }
Run Code Online (Sandbox Code Playgroud)
要么
if(sender.GetType().ToString() == "System.Forms.DataGridView") { .. }
Run Code Online (Sandbox Code Playgroud)
我对C++/CLI知之甚少,但我有一个需要解决方案的简单问题.我有一个C++/CLI类方法,它接受一个字节数组作为参数.该数组具有预定长度,可以事先在C#中分配.该阵列应该由C++/CLI方法填充数据.
如何声明方法然后从C#调用它?
我尝试过在C++/CLI类中使用以下内容:
public ref class C
{
public:
void FillBytes(array<BYTE^>^ bytes);
};
Run Code Online (Sandbox Code Playgroud)
然后,在C#中:
o = new C();
var bytes = new byte[3];
o.FillBytes(bytes);
Run Code Online (Sandbox Code Playgroud)
但那根本不起作用:).
在我的表单构造函数中创建CustomPropertyList类.
form(String ^s)
{
InitializeComponent();
CustomPropertyList ^propertyList = gcnew CustomPropertyList(s);
...
Run Code Online (Sandbox Code Playgroud)
CustomPropertyList类有一个析构函数
CustomPropertyList::~CustomPropertyList()
{
if (MessageBox::Show("Do you want to save your changes?","Editin",MessageBoxButtons::YesNo)==DialogResult::Yes)
...
Run Code Online (Sandbox Code Playgroud)
程序退出时为什么不调用它?(我知道这不是因为我没有看到消息框,那里有一个断点)
我非常感谢任何帮助
我复制
if ((MessageBox::Show(
"Are you sure that you would like to close the form?",
"Form Closing", MessageBoxButtons::YesNo,
MessageBoxIcon::Question) == DialogResult::No))
{
// cancel the closure of the form.
Application::Exit();
}
Run Code Online (Sandbox Code Playgroud)
来自msdn.我编译的地方我有
1> ------ Build build:项目:test2,配置:调试Win32 ------ 1> test2.cpp 1> c:\ users \kredkołamacz\ documents\visual studio 2010\projects\test2\test2\Form1.h(103):错误C2039:'否':不是'System :: Windows :: Forms :: Form :: DialogResult'1> c:\ users \kredkołamacz\ documents\visual studio 2010 \的成员projects\test2\test2\Form1.h(16):查看'System :: Windows :: Forms :: Form :: DialogResult'1> c:\ users \kredkołamacz\ documents\visual studio 2010\projects\test2 \的声明test2\Form1.h(103):错误C2065:'否':未声明的标识符==========构建:0成功,1失败,0最新,0跳过===== =====
怎么了?怎么解决这个问题?
在我的混合解决方案(C++/.NET)中,我想将Log4Net的ILog实例传递给C++对象(统一解决方案中的日志记录).
我想写一个C++接口(LogInterface),然后在C++/CLI项目中实现托管到非托管的适配器,但问题是非托管适配器将无法存储指向托管ILog的字段实例.
是否有一种优雅,安全且有效的方法来实现这一点(除了将托管引用存储在静态缓存中)?
class UnmanagedLogAdapter :
public LogInterface
{
public:
UnmanagedLogAdapter(log4net::ILog^ log);
virtual bool IsInfoEnabled(void) const override
{
return m_log->IsInfoEnabled();
}
virtual void Info(const std::wstring& message) override
{
log4net::ILog^ log = m_log; // alternative that I want to avoid: log = StaticCache::Find(m_logId);
log->Info(gcnew System::String(message.cstr()));
}
private:
log4net::ILog^ m_log; //TODO: a managed field is forbidden
};
Run Code Online (Sandbox Code Playgroud) 我的情况是这样的.
我有3个项目.
PrivacyDetectorDLL PDWrapper WindowsFormsApplication1(C#)
我的目标是在C#中使用Native C++类.所以我使用了C++/Cli包装类.但是我得到了保护级别的错误.我不明白.
我收到这个错误!
Error 1 'PDWrapper.PDWrapperClass.m_pCPrivacyDetectEngine' is inaccessible due to its protection level K:\Visual Studio 2010 Project\PrivacyDetecter\WindowsFormsApplication1\Form1.cs 23
Run Code Online (Sandbox Code Playgroud)
来自WindowsFormsApplication1项目的我的c#代码是这样的.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public PDWrapper.PDWrapperClass m_PDWrapper = null;
public Form1()
{
m_PDWrapper = new PDWrapper.PDWrapperClass();
unsafe
{
m_PDWrapper.m_pCPrivacyDetectEngine->initEngine();
}
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的PDWrapper类看起来像这样.
// PDWrapper.h
#pragma once
using namespace System; …Run Code Online (Sandbox Code Playgroud) 我试图用以下方式在c ++中创建一个名称空间:
namespace MyCompany.Library.Myproduct {
public ref class ClassWrapper
{
};
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Error 1 error C2059: syntax error : '.' ClassWrapper.h 7 1 MyCompany.Library.Myproduct
Run Code Online (Sandbox Code Playgroud)
为什么我不能拥有.在命名空间?
此命名空间定义位于c ++/cli中,将用于c#代码.在C#中,这个命名空间是有效的,但它似乎在c ++中无效.如何在c ++/cli代码中定义c#兼容的命名空间?
我有一个混合语言(C#,C++/CLI,本机C++)应用程序,我通过将日志语句放在几个对象的构造函数和析构函数中来跟踪某些对象的生命周期.我有一个C#对象,其中包含对C++/CLI对象的引用.
public class MyC#Class
{
public MyC#Class() { log.Debug("created MyC#Class object " + this.GetHashCode()); }
~MyC#Class() { log.Debug("destroyed MyC#Class object " + this.GetHashCode()); }
private MyC++Class myC++Obj = new MyC++Class();
}
MyC++Class::MyC++Class() { loggerInt1(LOGDEBUG, "created MyC++Class object %d", (int)this->GetHashCode());
MyC++Class::~MyC++Class() { loggerInt1(LOGDEBUG, "destroyed MyC++Class object %d", (int)this->GetHashCode());
Run Code Online (Sandbox Code Playgroud)
问题是当C#对象被垃圾收集时,我看到来自C#析构函数的日志语句,但是我没有看到来自C++/CLI对象的日志语句.换句话说,我看到"已销毁的MyC#类对象XXXX"但我没有看到相应的"销毁的MyC++类对象YYYY".
正如我对C#和C++/CLI所理解的那样,析构函数会覆盖GC通常会调用的Finalize()方法,因此当MyC++ Class对象被垃圾回收时,将打印MyC++类析构函数日志语句.
除了MyC++ Class对象被其他东西引用,因此还没有时间用于GC,是否有人知道为什么没有打印MyC++ Class析构函数日志语句?
谢谢,
是否有任何要求您使用C++/CLI的任务,并且无法在托管代码中完成(使用P/Invoke和Marshal类),性能除外?
c++-cli ×10
c# ×4
.net ×3
visual-c++ ×3
c++ ×2
destructor ×1
log4net ×1
logging ×1
namespaces ×1