[英]How do i create a namespace and constructor in c++ in a dll project?
I created on my visual studio 2012 pro a new dll project and the main .cpp file is empty except this line: 我在visual studio 2012 pro上创建了一个新的dll项目,除以下内容外,主.cpp文件为空:
#include "stdafx.h"
In this dll project I have a new c language item(module) I added with some functions inside. 在这个dll项目中,我添加了一个新的C语言项目(模块),并在其中添加了一些功能。
In fact I want to create in my main .cpp file some functions that will call the function from the c item(module). 实际上,我想在主.cpp文件中创建一些函数,这些函数将从c item(module)调用该函数。
For example in the .cpp file I will have something like this: 例如,在.cpp文件中,我将具有以下内容:
void start()
{
encoder.start();
}
Then in the .cpp I need to add a constructor so I can call there the start() 然后在.cpp文件中,我需要添加一个构造函数,以便可以在其中调用start()
How should I do it ? 我该怎么办?
Here is an example in my solution i have two projects one console application one dll. 这是我的解决方案中的一个示例,我有两个项目,一个控制台应用程序,一个dll。 This is the content of the main cpp file from the console application project: 这是控制台应用程序项目中主要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 is a function from this c item(file/module) i created in the console application project. vide_encode_example是我在控制台应用程序项目中创建的此c项(文件/模块)的函数。 I have a file called example.c and the video_encode_example is in the example.c 我有一个名为example.c的文件,video_encode_example在example.c中
Now i added to the solution a new dll project and the main.cpp file is empty except the line: #include "stdafx.h" 现在,我向解决方案中添加了一个新的dll项目,并且main.cpp文件为空,除了以下行:#include“ stdafx.h”
What i want to do in this dll project in the main.cpp is two things: 我要在main.cpp中的此dll项目中做的两件事:
To create some function for example 例如创建一些功能
void thisstart() { } 无效thisstart(){}
Then i want to in this start function to call a start() function which is in ac file/module i created in the dll project. 然后,我想在此启动函数中调用start()函数,该函数位于dll项目中创建的ac文件/模块中。
So it should look like: 所以它应该看起来像:
void thisstart()
{
start();
}
Where start(); 在哪里start(); is from the c module/file 来自c模块/文件
Then i'm going to use this dll in c# and in c# i want to be able to use the thisstart() function. 然后,我将在c#中使用此dll,在c#中,我希望能够使用thisstart()函数。
EDIT 编辑
This is the main.h content: 这是main.h的内容:
namespace dllproj{
extern "C" void start();
void thisstart();
}
I'm getting and two errors now on dllproj: 我在dllproj上遇到两个错误:
Then this is the cpp file content now: 这就是现在的cpp文件内容:
#define dllproj;
#include "stdafx.h"
#include "targetver.h"
#include "main.h"
void thisstart()
{
dllproj;::start();
}
And i'm getting two errors: 而且我遇到两个错误:
Please show me the complete solution and explain to me also which variable later in CSHARP i will use with the dll to make an instance for it and to call this function/s in the cpp ? 请向我展示完整的解决方案,并向我说明稍后在CSHARP中哪个变量将与dll一起使用以为其创建实例并在cpp中调用此函数。
In csharp for example when i add the dll : test = new something(); 例如在csharp中,当我添加dll时:test = new something(); then test.thisstart(); 然后test.thisstart();
From the comments "start() is in the dll project in a (c language file i create test.c)" 从注释“ start()在dll项目的(我创建test.c的c语言文件中)”中
1) create a header file eg main.h and add the following 1)创建一个头文件,例如main.h并添加以下内容
namespace dllproj{
extern "c"
{
extern void start();
}
void thisstart();
}
2)add main.h to main.cpp and define thisstart()
2)将main.h添加到main.cpp并定义thisstart()
void dllproj::thisstart()
{
dllproj::start();
}
make sure start() is declared with __declspec(dllexport)
in the dll. 确保在dll中使用__declspec(dllexport)
声明了start()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.