简体   繁体   English

如何在同一解决方案中的项目之间共享变量?

[英]How to share variable between projects in the same solution?

How to share variable between two projects in the same solution?如何在同一解决方案中的两个项目之间共享变量? I've tried to use extern, static, getters/setters and nothing seems to work.我尝试使用 extern、static、getter/setter,但似乎没有任何效果。 What I want to achieve is to set work variable in in project and pass it's value to another project called worker so that it can use it and do some calculations.我想要实现的是在项目中设置工作变量并将其值传递给另一个名为 worker 的项目,以便它可以使用它并进行一些计算。 This is the code:这是代码:

Solution is named Sharing:解决方案名为共享:

Sharing/MainApp:共享/主应用程序:

MainApp.h主应用程序.h

#pragma once

#include "resource.h"

MainApp.cpp:主应用程序.cpp:

#include "framework.h"
#include "Sharing.h"
#include "../Worker/Worker.h"
...
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   work = 33; //<- here I want to assign value and pass it to Worker project

   hInst = hInstance;
   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

Sharing/Worker:共享/工作者:

Worker.h:工人.h:

#ifdef WORKER_EXPORTS
#define WORKER_API __declspec(dllexport)
#else
#define WORKER_API __declspec(dllimport)
#endif

// This class is exported from the dll
class WORKER_API CWorker {
public:
    CWorker(void);
    // TODO: add your methods here.
};

extern WORKER_API int nWorker;
extern int work; // <- this does not work

WORKER_API int fnWorker(void);

Worker.cpp:工人.cpp:

// Worker.cpp : Defines the exported functions for the DLL.
//

#include "pch.h"
#include "framework.h"
#include "Worker.h"


// This is an example of an exported variable
WORKER_API int nWorker=0;
int work; // <- here

// This is an example of an exported function.
WORKER_API int fnWorker(void)
{
    return 0;
}

// This is the constructor of a class that has been exported.
CWorker::CWorker()
{
    return;
}

Compilation error:编译错误:

Error   LNK2001 unresolved external symbol "int work" (?work@@3HA)  Sharing C:\..\..\..\..\..\Sharing\Sharing.obj   1

Error   LNK1120 1 unresolved externals  Sharing C:\..\...\..\..\..\Debug\Sharing.exe    1   

Differenf projects (and running processes) have different memory.不同的项目(和正在运行的进程)有不同的 memory。 You should use "external" methods.您应该使用“外部”方法。 For example, shared memory: https://www.boost.org/doc/libs/1_54_0/doc/html/interprocess/sharedmemorybetweenprocesses.html例如,共享 memory: https://www.boost.org/doc/libs/1_54_0/doc/html/interprocess/sharedmemorybetweenprocesses.ZFC35FDC70D5FC69D7A369883A82ECZ2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一解决方案中的项目之间传递CString - Passing CStrings between projects in same solution 如何在 Visual Studio 2022 上的单个解决方案中的项目之间共享库文件/包含文件夹? - How to share Library files/ include folders between projects in a single solution on Visual Studio 2022? 如何在c ++项目之间共享类? - how to share class between c++ projects? 同一解决方案中不同项目之间的引用(Visual Studio 2012) - Reference between different projects in the same solution (Visual Studio 2012) 同一解决方案中项目之间的链接问题(Exe依赖于Lib) - Link problem between projects in same solution (Exe depends on Lib) 如何在一个解决方案的两个独立项目中创建两个具有相同名称的类? - How to create 2 classes with the same name in two separate projects in one solution? 如何在同一解决方案下为多个项目声明通用的#define? - how to declare common #define for several projects under the same solution? 如何在同一个解决方案中包含其他项目的头文件? - How to include header files of another projects in the same solution? 无法在同一解决方案中的 2 个项目之间链接,并且我没有用于其他依赖项的 .Lib 文件 - Cannot Link between 2 projects in the same solution, and i dont have a .Lib File for additional dependencies 在同一解决方案中将头文件用于多个项目 - Using header files with multiple projects in same solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM