通过C库实现C#应用程序之间的数据共享



我这里有一个非常具体的问题,我在实践中从未见过,我希望你能帮助我展示我做错了什么:

因此,whe有两个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;
}

和类似的返回hbStatusint GetHBStatus(),以及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)中导入SetHBStatusGetHBExit,并具有以下逻辑:

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应用程序去初始化并杀死它自己,我们将从master调用,而我只在一个地方进行这个调用,并且存在问题(库中的hbStatus有"1"标志,在其Deini中,它将hbStatus设置为"0"),它期望master,如下所示:

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上结束并关闭自己,就像收到的Exit标志一样,但标志NOTHING和NOBODY通过调用适当的函数设置(并且没有显示关于断开连接和杀死的消息)。

当我尝试将SetHBExit导入HB.exe,并在初始化后使用false调用时,问题仍然出现。

这只能在32位的应用程序和库中看到,如果我将其编译到64位版本,应用程序就会顺利运行,并根据需要运行。但是,我不能使用64位版本,因为应用程序是为我的客户准备的,他不能在64位版本中运行它(这也是一个奇怪的问题,他在64位中有一个W7,但在程序尝试首先调用库函数时收到BadImageFormatException(在我的机器中它正常运行。奇怪,奇怪)

有什么建议我错了吗?

更新&可能的解决方案:

"Old"C不支持"bool"类型,如果我使用bool,即typedef int,则32位应用程序可以正常运行。所以我认为这个问题已经解决了:)

64位机器上的64位应用程序的次要问题仍然没有解决,BadImageFormat异常,但在我的机器上它运行得很好

最新更新