[英]How do i create a namespace and constructor in c++ in a dll project?
我在visual studio 2012 pro上创建了一个新的dll项目,除以下内容外,主.cpp文件为空:
#include "stdafx.h"
在这个dll项目中,我添加了一个新的C语言项目(模块),并在其中添加了一些功能。
实际上,我想在主.cpp文件中创建一些函数,这些函数将从c item(module)调用该函数。
例如,在.cpp文件中,我将具有以下内容:
void start()
{
encoder.start();
}
然后在.cpp文件中,我需要添加一个构造函数,以便可以在其中调用start()
我该怎么办?
这是我的解决方案中的一个示例,我有两个项目,一个控制台应用程序,一个dll。 这是控制台应用程序项目中主要cpp文件的内容:
#include "stdafx.h"
#include "targetver.h"
extern "C" {
void video_encode_example(const char *filename);
}
int _tmain(int argc, _TCHAR* argv[])
{
video_encode_example("adi.avi");
return 0;
}
vide_encode_example是我在控制台应用程序项目中创建的此c项(文件/模块)的函数。 我有一个名为example.c的文件,video_encode_example在example.c中
现在,我向解决方案中添加了一个新的dll项目,并且main.cpp文件为空,除了以下行:#include“ stdafx.h”
我要在main.cpp中的此dll项目中做的两件事:
例如创建一些功能
无效thisstart(){}
然后,我想在此启动函数中调用start()函数,该函数位于dll项目中创建的ac文件/模块中。
所以它应该看起来像:
void thisstart()
{
start();
}
在哪里start(); 来自c模块/文件
然后,我将在c#中使用此dll,在c#中,我希望能够使用thisstart()函数。
编辑
这是main.h的内容:
namespace dllproj{
extern "C" void start();
void thisstart();
}
我在dllproj上遇到两个错误:
这就是现在的cpp文件内容:
#define dllproj;
#include "stdafx.h"
#include "targetver.h"
#include "main.h"
void thisstart()
{
dllproj;::start();
}
而且我遇到两个错误:
请向我展示完整的解决方案,并向我说明稍后在CSHARP中哪个变量将与dll一起使用以为其创建实例并在cpp中调用此函数。
例如在csharp中,当我添加dll时:test = new something(); 然后test.thisstart();
从注释“ start()在dll项目的(我创建test.c的c语言文件中)”中
1)创建一个头文件,例如main.h并添加以下内容
namespace dllproj{
extern "c"
{
extern void start();
}
void thisstart();
}
2)将main.h添加到main.cpp并定义thisstart()
void dllproj::thisstart()
{
dllproj::start();
}
确保在dll中使用__declspec(dllexport)
声明了start()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.