我這裏有非常具體的問題,我不能在我的實踐中見過的,我希望你能幫助我展示一下我做錯了:數據共享
所以,磨片有兩個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庫函數(在我的機器上運行正常奇怪,奇怪的。)
在那裏我是錯的任何建議