簡體   English   中英

如何在DLL項目中的C ++中創建名稱空間和構造函數?

[英]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項目中做的兩件事:

  1. 例如創建一些功能

    無效thisstart(){}

然后,我想在此啟動函數中調用start()函數,該函數位於dll項目中創建的ac文件/模塊中。

所以它應該看起來像:

void thisstart()
  {
    start();
  }

在哪里start(); 來自c模塊/文件

然后,我將在c#中使用此dll,在c#中,我希望能夠使用thisstart()函數。

編輯

這是main.h的內容:

namespace dllproj{

    extern "C" void start();
    void thisstart();
}

我在dllproj上遇到兩個錯誤:

  1. 錯誤2錯誤C2054:預期'('跟隨'命名空間'
  2. 4 IntelliSense:應為標識符

這就是現在的cpp文件內容:

#define dllproj;

#include "stdafx.h"
#include "targetver.h"
#include "main.h"

void thisstart()
 {
     dllproj;::start();
 }

而且我遇到兩個錯誤:

  1. 在定義行上:錯誤1錯誤C2008:';' :宏定義意外
  2. 在dllproj; :: start();上 錯誤3錯誤C2143:語法錯誤:缺少';' 在“:”之前

請向我展示完整的解決方案,並向我說明稍后在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM