我有一个本机/非托管DLL,它有一个"CreateObject"函数,它返回一个指向业务对象的指针..所以调用将是...... 喜欢:
[DllImport("MyDll.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateObject();
Run Code Online (Sandbox Code Playgroud)
问题:对象是暴露我想要调用的"Connect()"之类的公共函数,但我不知道如何"映射"这些调用,所以我有一个简单的方法签名,如:
private bool Connect();
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我试图找出VisualHint.SmartPropertyGrid.PropertyGrid是否满足我设置属性的需要,我的一些属性是布尔值.
其中一个参数是
// container:
// The instance of an object containing the C# property displayed in this new
// property.
Run Code Online (Sandbox Code Playgroud)
所以一般来说,我可以把它交给一个合适的对象 - 但是,布尔值是值类型,而不是对象.
在C#中,正确的语法是输入容器typeof(bool).有没有明智的方法在C++中做到这一点,还是我必须制作自己的bool对象?
我正在构建一个素数查找器,并且遇到内存问题:
这可能是由于堆的损坏,这表明PrimeNumbers.exe或它已加载的任何DLL中的错误.
PS.请不要告诉我,如果这不是寻找素数的方法,我想自己搞清楚!
码:
// PrimeNumbers.cpp : main project file.
#include "stdafx.h"
#include <vector>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Until what number do you want to stop?");
signed const int numtstop = Convert::ToInt16(Console::ReadLine());
bool * isvalid = new bool[numtstop];
int allattempts = numtstop*numtstop; // Find all the possible combinations of numbers
for (int currentnumb = 0; currentnumb <= allattempts; currentnumb++) // For each number try to find a combination
{
for (int i = …Run Code Online (Sandbox Code Playgroud) 如何更改 Winform C++/CLI 项目中的应用程序(二进制文件)图标?我正在使用 MS VS 2010。
我试图转到项目属性,但没有找到Application选项卡:

和这里资源选项:

使用C#和C++/CLI开始使用互操作的最佳在线指南/教程是什么?我正在寻找一个很好的指南,将C#中的结构传递给C++/CLI.我见过的许多教程都没有涉及将C#中的自定义结构传递给C++/CLI.有什么建议 ?
谢谢.
我试图从非托管代码调用托管方法.但是,托管代码要求我使用'(__ clrcall)调用约定,而我的非托管C++代码拒绝让我使用__clrcall调用约定而不使用/ clr选项.我不相信我想这样做,因为非托管项目不是我可以改为托管的.
正如我在CodeGuru和MSDN上看到的那样,我已经完成了在托管端构建所有这些委托和函数指针编组,但是除非我使用不能成为ref类成员的__stdcall约定调用静态函数,否则此错误仍然会弹出.该方法需要是ref类的实例.
我能解决这个问题的唯一方法是从程序集中的非托管代码执行我的方法调用,在调用之后跳过ESP寄存器(添加4)的弹出(调用在参数中有一个参数,因此'4 ").这让我有效地做了__clrcall.那个选项很难找到.
有人对如何解决这个问题有任何想法?肯定是可能的.我遗漏了一些简单而重要的信息.
这是我的委托定义(所有这些都属于单个ref类ManagedSensor):
delegate void OxpOnReceivedMeasurement(std::vector<OBX> *obxes);
Run Code Online (Sandbox Code Playgroud)
这是我想要调用的实例方法
void LocalOxpMeasurementCallback(std::vector<OBX> *obxes);
Run Code Online (Sandbox Code Playgroud)
这里是ref类函数对ref class构造函数中完成的非托管函数的编组:
// Now to make the 'delegate' unmanaged function pointer usable for unmanged code
// The argument is the unmanaged callback.
OxpOnReceivedMeasurement ^oxpMeasurementCallbackFP = gcnew OxpOnReceivedMeasurement(this, &ManagedSensor::LocalOxpMeasurementCallback);
// Lock it so the garbage collector doesnt get rid of it or move it
gch = GCHandle::Alloc(oxpMeasurementCallbackFP);
// Pass the marshaled function pointer to the unmanaged code
IntPtr ip = Marshal::GetFunctionPointerForDelegate(oxpMeasurementCallbackFP);
// fnOnReceiveMeasurement is defined …Run Code Online (Sandbox Code Playgroud) 我正在使用 Monitor 类来管理关键部分。但有时我会遇到SynchronizationLockException以下消息
An unhandled exception of type 'System.Threading.SynchronizationLockException' occurred in ManagedType.dll
Additional information: Object synchronization method was called from an unsynchronized block of code.
Run Code Online (Sandbox Code Playgroud)
我的代码在下面
Object^ lockObj = gcnew Object(); //Global variable
bool GetValue()
{
try
{
Monitor::Enter(lockObj);
return this.value;
}
finally
{
Monitor::Exit(lockObj);
}
}
Run Code Online (Sandbox Code Playgroud)
在SynchronizationLockException抛出时Monitor::Exit(lockObj)调用。
我已经在这个论坛和互联网上搜索过,但包括 MSDN 在内的大多数主题都说“当您尝试在 Monitor.Enter() 和 Monitor.Exit() 中使用值类型变量而不是引用类型时,将抛出此异常”。
但就我而言,我使用 lockObj 作为引用对象。所以我不知道为什么它会抛出这个异常。有人可以帮助我吗?
非常感谢,
T&T集团!
假设我有一个 c++ 非托管类,看起来像这样
#include "note.h"
class chord
{
private:
note* _root; // or, as in my real class: std::shared_ptr<note> _root;
// _third, _fifth, _seventh, etc
public:
someClass(const note n1, const note n2, const note n3); // constructor takes some of these notes to make a chord
std::shared_ptr<note> root() const; // returns ptr to root of the chord
std::string name() const; // returns the name of this chord
}
Run Code Online (Sandbox Code Playgroud)
现在,我知道我需要将这两个类都包装到 cli 中的托管类中。但问题是,如何将指向本机类的私有指针传递给构造函数?
就目前而言, Note* _src 在 noteWrapper 中是私有的。但是原生 Chord() 需要原生 Note …
我已经查看过很多在线搜索,大多数都有C#与C或C++一起讨论C#的例子,但从未遇到过使用C++调用C DLL(特别是在我的情况下使用带有WPF的C++/CLI).
有人可以用一个例子来解释如何在C++中调用C DLL函数.我有一个C DLL,其所有函数都定义为extern"C"funcName(),还有一个导出函数.def文件,其中包含所有需要导出的函数名.现在有了这个C DLL,我如何在C++代码中调用它的导出函数.
谢谢.
我有一个用C#编写的MMC管理单元.似乎MMC为每个托管管理单元创建了一个单独的AppDomain.它还有一个默认的AppDomain,用于托管系统dll,如mscorlib.dll,Microsoft.ManagementConsole.dll等.
我的管理单元有一个本机C++ DLL,它可以创建可以通过Interop调用托管代码的本机线程.问题是当本机线程访问我的托管代码时,它尝试在默认的AppDomain中执行它,而不是我的管理单元.
有没有办法强制本机线程"切换"到管理单元的AppDomain?我无法重写本机dll.我唯一能做的就是在C++/CLI中实现一些这个dll会调用的C++接口.
最小,完整和可验证的示例如下.要编译它,请在Visual Studio中选择C++/CLR控制台应用程序项目类型.
#include <Windows.h>
#include <msclr/gcroot.h>
using namespace System;
#pragma unmanaged
class IService
{
public:
virtual void Operate() = 0;
};
DWORD __stdcall MyNativeThread(LPVOID arg)
{
IService* service = (IService*)arg;
service->Operate();
return 0;
}
void StartNativeThread(IService* service)
{
CloseHandle(CreateThread(NULL, 0, &MyNativeThread, service, 0, NULL));
}
#pragma managed
public ref class ServiceManagedImpl
{
public:
void Operate()
{
System::Console::WriteLine("ServiceManagedImpl::Operate: Domain: {0}", System::AppDomain::CurrentDomain->Id);
}
};
class ServiceImpl : public IService
{
public:
ServiceImpl(ServiceManagedImpl^ managedImpl)
{
m_managedImpl = managedImpl;
} …Run Code Online (Sandbox Code Playgroud)