在C#中使用带有头文件的C ++ DLL [英] Using C++ dll having header file in C#

查看:213
本文介绍了在C#中使用带有头文件的C ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我想在C#程序中使用C ++ dll(有头文件)。



请在下面找到头文件。



I am totally new in C++ and I want to use C++ dll (having header file) in C# program.

Please find below header file.

#pragma once
#define DAILY_PERIOD 24*60
#define MIN_PERIOD 1

#ifdef API_DLL 
#define METHOD_TYPE __declspec(dllexport)
#else
#define METHOD_TYPE __declspec(dllimport)
#endif

struct Quote {
unsigned long Date;
float Price;
float Open;
float High;
float Low;
float Volume;
float OpenInterest; 
};

class METHOD_TYPE CMinuteApiCallback
{
public: 

virtual int quote_notify( const char* symbol, int interval, int nMaxSize, Quotation *pQuotes, unsigned long echo)=0;

};


class METHOD_TYPE CMinuteApi
{
public:
CMinuteApi(void);

int Initialise(char *serialkey, CMinuteApiCallback* callback);

int GetQuote(char * symbol, int periodicity, unsigned long lasttimeupdate, unsigned long echo);

int DeleteQuote(char * symbol, int periodicity);

~CMinuteApi(void);
};





请告诉我如何在C#程序中调用所有这些方法。



提前致谢。



已添加代码块[/编辑]



So please let me know how can I call all these methods in my C# program.

Thanks in advance.

Code block added[/Edit]

推荐答案

您可以使用Dllimport在C ++ DLL文件中使用函数,



请关注该链接 DLLIMPORT



问候。 ..
You can use the the Dllimport to use functions in your C++ dll files,

please follow that link DLLIMPORT

Regards...


您好,



使用平台调用(P / Invoke):

Essential P / Invoke [ ^ ]

P / Invoke教程:基础知识(第1部分) [ ^ ]

http://msdn.microsoft.com/en-us/magazine/cc164123.aspx [ ^ ]
Hi,

Use Platform Invoke (P/Invoke):
Essential P/Invoke[^]
P/Invoke Tutorial: Basics (Part 1)[^]
http://msdn.microsoft.com/en-us/magazine/cc164123.aspx[^]


如果您不熟悉C ++,那么可能不是最好的想法是创建一个使用这两种语言的程序。



此外,您的C ++代码不使用与C#相同的字符集,因此可能会出现国际化问题。



此外,您的声明在从构造函数和析构函数开始使用C#以及使用const修饰符的不一致的上下文中存在许多问题。特别是,您必须修改接口,以便通过C ++ DLL完成内存分配。同样用于调用析构函数并在最后释放内存。



我认为为了简单起见,最好在C#中实现所有内容。



如果你真的需要一些C ++代码(例如重用现有代码需要几个小时来移植),使用混合模式C ++(默认模式下的C ++ / CLI:/ clr)对于一个中间DLL可能是最简单的路径......但你仍然有一些高级的东西要学习,比如使用gc_root。
If you are new to C++, then it might not the best idea to make a program that use both languages.

Also your C++ code does not use the same charset as the C# one so it might be problematic for internationalization.

Also your declaration have many problems in the context of being used from C# starting with constructor and destructor and also with inconsistent usage of const modifier. In particular, you would have to modify the interface so that the allocation of memory would be done by the C++ DLL. Same for calling destructor and freeing memory at the end.

I think it would be best to implement everything in C# for simplicity purpose.

If you really need some C++ code (for example to reuse existing code that would require more than a few hours to port), the using mixed mode C++ (C++/CLI in default mode: /clr) for an intermediate DLL might be the easiest route... but you still have some advanced stuff to learn like using gc_root.


这篇关于在C#中使用带有头文件的C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆