c++ - 如何在同一解决方案中的项目之间共享变量?
问题描述
如何在同一解决方案中的两个项目之间共享变量?我尝试使用外部、静态、getter/setter,但似乎没有任何效果。我想要实现的是在项目中设置工作变量并将其值传递给另一个名为 worker 的项目,以便它可以使用它并进行一些计算。这是代码:
解决方案名为共享:
共享/主应用程序:
主应用程序.h
#pragma once
#include "resource.h"
主应用程序.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;
}
共享/工作者:
工人.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);
工人.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;
}
编译错误:
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
解决方案
不同的项目(和正在运行的进程)具有不同的内存。您应该使用“外部”方法。例如共享内存:https ://www.boost.org/doc/libs/1_54_0/doc/html/interprocess/sharedmemorybetweenprocesses.html
推荐阅读
- angular - 如何在 td 中实现 ngIf
- python - 如何从Python中的字符串中取出前2位数字
- c# - c# Should I use a service or just poll the database
- angular - How to add new rows inside of *ngFor loop? I Want to Implement expanding collapsing functionality inside of the table
- android - 如何在左侧操作栏上膨胀菜单
- java - Couldn't load class com.mysql.jdbc.Driver by class.forName("com.mysql.jdbc.Driver")
- angular - Angular 5:如何将事件从 authguard 广播到标头?
- python - 使用 Python 库的问题 - Pyotodom
- android - How can I create .vcf file for a single contact?
- sql - postgresql中不同产品的打开和关闭数量查询