C库在C#应用程序之间共享数据

时间:2013-02-10 10:28:12

标签: c# c console-application data-interchange

我这里有非常具体的问题,我在练习中看不到,我希望你能帮我展示我做错了什么:

因此,有两个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,以及hbExit一个的getter和setter

在主应用程序(名为“master”)中是导入状态getter和exit 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和THIS调用我只在一个地方进行调用,并且存在问题(库中的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返回成功标志,master将继续正常工作,但HB突然在Deinit结束并关闭自己,收到退出标志,但标志NOTHING和NOBODY通过调用适当的函数设置(并且不显示关于断开连接和查杀的消息)。

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

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

我有什么不对的建议吗?

1 个答案:

答案 0 :(得分:0)

更新&amp;可能的解决方案:

“Old”C不支持“bool”类型,如果我使用BOOL,它是typedef int,32位应用程序正常运行。所以我正在考虑解决这个问题:)

64位机器上仍然存在64位应用程序的二级问题仍未解决,BadImageFormat异常,但在我的机器上它运行良好