我这里有非常具体的问题,我不能在我的实践中见过的,我希望你能帮助我展示一下我做错了:数据共享
所以,磨片有两个C-sharp控制台应用程序以及一个共享内存段并用于数据(即状态位)交换的库。
在图书馆它(为简单起见,我不能发布完整的代码,有点点的东西)看起来是这样的:
#pragma data_seg("shared")
int hbStatus = 0;
bool hbExit = false;
#pragma data_seg()
#pragma comment(linker, "/SECTION:shared,RWS")
extern "C" __declspec(dllexport) void __stdcall SetHBStatus(int status)
{
hbStatus = status;
}
与同类int GetHBStatus()
返回hbStatus
,和getter和setter为hbExit
一个
在主应用程序(称为 “主站”)被导入状态getter和出口调节器,像这样
class Program
{
const string dllimport = @"interchange.dll";
[DllImport(dllimport)]
public static extern int GetHBStatus();
[DllImport(dllimport)]
public static extern void SetHBExit(bool value);
...
在从应用(HB.exe)进口SetHBStatus
和GetHBExit
,并有这样的逻辑:
static void Main(string[] args)
{
Initialize(); //after initialize, SetHBStatus is set to 1 if succeed, otherwise -1
InitializeHeartbeatTimer(); //timer period is set in initialize and thorows an events, which doing main logic, so we may have empty while cycle
while (!GetHBExit()) { }
Console.WriteLine("HB exit status: " + GetHBExit());
Console.ReadLine();
Deinitialize();
hbTimer.Dispose();
此状态用于主应用程序进行:
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("SUCCESS!");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Invoking heartbeat application");
try
{
Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "\\HB.exe");
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Failed to invoke HB application. Ending...");
Console.WriteLine("Message: " + e.Message);
break;
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("SUCCESS!");
SetHBExit(false); //clearing junk in library, if any
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Waiting for HB app initializes");
while (GetHBStatus() == 0) //waiting for response HB.exe
{ }
if (GetHBStatus() > 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("SUCCESS! Heartbeat send and ticking by interval");
}
if (GetHBStatus() < 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("HB application fails. Check HB_log.txt for more details. Ending...");
break;
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Generating file for.....
...
如果我们需要告诉HB应用程序去初始化和自己杀死,我们将打电话给主人和这个电话我只在一个地方做,并且有问题(hbStatus
库中有“1”标志,在其Deini
t是hbStatus
设置为“0 “),期待大师,像这样:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Disconnecting heartbeat");
SetHBExit(true);
while (GetHBStatus() > 0) { }
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("HB killed and disconnected");
Console.WriteLine("\nAll disconnected, invoking main menu");
ShowOptions();
现在是我的问题:当我执行的主应用程序,它调用HB.exe,让它初始化,HB返回成功标志,主人将继续正常工作,但HB突然DEINIT和关闭结束本身,作为收到的退出标志,但NOTHING和NOBODY标志通过调用适当的功能设置(以及关于断开和查杀的消息未示出)。
当我尝试将SetHBExit导入到HB.exe并在初始化后调用false时,问题仍然出现。
这只能在32位应用程序和库中看到,如果我将它编译为64版本,应用程序可以顺利运行并且按需要运行。但是,我无法使用64位版本,因为应用程序适用于我的客户端,他们无法在64位版本中运行它(这也是一个奇怪的问题,他有一个64位的W7,但在程序尝试第一次调用时收到BadImageFormatException库函数(在我的机器上运行正常奇怪,奇怪的。)
在那里我是错的任何建议