2014-03-29 133 views
0

我有一個解決方案,其中包含三個項目。一個是創建靜態庫,即.lib文件。它包含一個頭文件main.h和一個main.cpp文件。 cpp文件包含頭文件功能的定義。變量在不同項目之間共享的頭文件

第二個項目是.exe項目,其中包含頭文件main.h並調用頭文件的函數。

第三個項目也是一個.exe項目,它包含頭文件並使用頭文件的變量標誌。

現在這兩個.exe項目都創建了不同的變量實例。但我想動態地在項目之間共享同一個變量實例。因爲我必須在同一時刻將一個項目生成的值映射到其他項目。

請在我接近我的項目截止日期前幫助我。

感謝您的幫助。

以下是部分代碼。

的main.cpp和main.h是的.lib項目

main.h 

extern int flag; 
extern int detect1(void); 

main.cpp 


#include<stdio.h> 
#include"main.h" 
#include <Windows.h> 
#include <ShellAPI.h> 
using namespace std; 
using namespace cv; 
int flag=0; 


int detect1(void) 
{ 
    int Cx=0,Cy=0,Kx=20,Ky=20,Sx=0,Sy=0,j=0; 
    //create the cascade classifier object used for the face detection 
    CascadeClassifier face_cascade; 
    //use the haarcascade_frontalface_alt.xml library 
    face_cascade.load("E:\\haarcascade_frontalface_alt.xml"); 
    //System::DateTime now = System::DateTime::Now; 
    //cout << now.Hour; 
    //WinExec("E:\\FallingBlock\\FallingBlock\\FallingBlock\\bin\\x86\\Debug\\FallingBlock.exe",SW_SHOW); 
    //setup video capture device and link it to the first capture device 
    VideoCapture captureDevice; 
    captureDevice.open(0); 

    //setup image files used in the capture process 
    Mat captureFrame; 
    Mat grayscaleFrame; 

    //create a window to present the results 
    namedWindow("capture", 1); 

    //create a loop to capture and find faces 
    while(true) 
    { 
     //capture a new image frame 
     captureDevice>>captureFrame; 
     //convert captured image to gray scale and equalize 
     cvtColor(captureFrame, grayscaleFrame, CV_BGR2GRAY); 
     equalizeHist(grayscaleFrame, grayscaleFrame); 

     //create a vector array to store the face found 
     std::vector<Rect> faces; 

     //find faces and store them in the vector array 
     face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3,  CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30)); 

     //draw a rectangle for all found faces in the vector array on the original image 
     for(unsigned int i = 0; i < faces.size(); i++) 
     { 

      Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height); 
      Point pt2(faces[i].x, faces[i].y); 

      rectangle(captureFrame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0); 
      if(faces.size()>=1) 
      j++; 
      Cx = faces[i].x + (faces[i].width/2); 
      Cy = faces[i].y + (faces[i].height/2); 
      if(j==1) 
      { 
       Sx=Cx; 
       Sy=Cy; 
       flag=0; 
      } 
     } 

     if(Cx-Sx > Kx) 
     { 
       flag = 1; 
       printf("%d",flag); 
     } 
     else 
     { 
      if(Cx-Sx < -Kx) 
      { 
       flag = 2; 
       printf("%d",flag); 
       //update(2); 
      } 
      else 
      { 
       if(Cy-Sy > Ky) 
       { 
        flag = 3; 
        printf("%d",flag); 
        //update(3); 
       } 
       else 
       { 
        if(Cy-Sy < -Ky) 
        { 
         flag = 4; 
         printf("%d",flag); 
         //update(4); 
        } 
        else 
         if(abs(Cx-Sx) < Kx && abs(Cy-Sy)<Ky) 
         { 
          flag = 0; 
          printf("%d",flag); 
          //update(0); 
         } 
       } 
      } 
     } 


2nd project's code 
face.cpp 

#include"main.h" 
#include<stdio.h> 
int main() 
{ 

    detect1(); 

}  

3rd project's code 
tetris.cpp 

#include"main.h" 

int key; 
key = flag; 
if(key==0) 
{ 
    MessageBox(hwnd,"Space2","TetRiX",0); 
} 
if(key==4) 
{ 
    tetris.handleInput(1); 
    tetris.drawScreen(2); 
    //MessageBox(hwnd,"Space2","TetRiX",0); 
} 
+0

這還不清楚。你想在建造時間還是運行時間分享價值? – user1329482

+0

@ user1329482我想在運行時共享變量值 – param

回答

0

的文件,你需要查找如何做到在操作系統下,你的應用程序將運行的進程間通信。 (此時我假設這些進程在同一臺計算機上運行。)它看起來像使用Windows(基於看到對「MessageBox」的調用),所以最簡單的方法是兩個進程都使用RegisterWindowMessage創建一個通常理解的消息值,然後使用PostMessage或SendMessage通過LPARAM發送數據。 (你需要每個人都得到另一個的窗口句柄,這很容易)。你需要在兩個進程中都有某種排除機制(互斥或關鍵部分),以確保共享值可以不能同時閱讀和書寫。如果兩個過程都可以做「改變和交換」,那麼如果兩者都試圖同時做到這一點,那麼你就會遇到一個有趣的問題,因爲你必須處理在共享值上發生死鎖的可能性。

您也可以使用共享內存,但這有點多。

如果進程位於不同的計算機上,則需要通過TCP/IP或TCP/IP之上的協議來完成。你可以使用pub-sub安排 - 或任何數量的東西。如果不瞭解你想要完成什麼,就很難知道要推薦什麼。 (據記載,在多進程/多線程操作系統中幾乎沒有辦法在「同一時刻」共享某些內容。「你可以任意關閉,但計算機不能像這樣工作)

鑑於涉及的難度,有沒有其他一些設計可以使這個更清潔?爲什麼這些流程必須以這種方式交換信息?必須使用單獨的流程來完成嗎?

相關問題