通过C库实现C#应用程序之间的数据共享
我这里有一个非常具体的问题,我在实践中从未见过,我希望你能帮助我证明我做错了什么: 因此,whe有两个C-sharp控制台应用程序和一个库,该库具有共享内存段,用于数据(即状态位)交换 在库中(为了简单起见,我不能发布完整的代码,还有更多的东西)如下所示:通过C库实现C#应用程序之间的数据共享,c#,c,console-application,data-interchange,C#,C,Console Application,Data Interchange,我这里有一个非常具体的问题,我在实践中从未见过,我希望你能帮助我证明我做错了什么: 因此,whe有两个C-sharp控制台应用程序和一个库,该库具有共享内存段,用于数据(即状态位)交换 在库中(为了简单起见,我不能发布完整的代码,还有更多的东西)如下所示: #pragma data_seg("shared") int hbStatus = 0; bool hbExit = false; #pragma data_seg() #pragma comment(linker, "/SECTION:sh
#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;
}
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();
和类似的int GetHBStatus()
返回hbStatus
,以及hbExit的getter和setter
在主应用程序中(名为“master”)导入了状态getter和出口setter,如下所示
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.exe,让它初始化,HB返回成功标志,主应用程序将继续正常工作,但HB突然在Deinit上结束并关闭自身,作为接收到的退出标志,但该标志没有任何设置,也没有人通过调用适当的函数来设置(未显示有关断开和终止的消息)
当我尝试将SetHBExit导入HB.exe,并在初始化后使用false调用时,问题仍然出现
这仅在32位应用程序和库上可以看到,如果我将其编译为64版本,应用程序运行平稳,并符合需要。但是,我不能使用64位版本,因为应用程序是为我的客户机提供的,客户机无法在64版本中运行它(这也是一个奇怪的问题,他有一个64位的W7,但在程序尝试首先调用库函数时收到BadImageFormatException(在我的机器中它运行正常。奇怪,奇怪)
有什么我错了的建议吗?更新和可能的解决方案:
“Old”C不支持“bool”类型,如果我使用bool,即typedef int,32位应用程序运行正常。因此,我认为这个问题已经解决:)
64位机器上的64位应用程序的第二个问题仍然没有解决,BadImageFormat异常,但在我的机器上它工作得很好