C++库可以在 vb6 中运行,但不能在 c# 中运行

发布于 2024-08-20 20:44:04 字数 1192 浏览 5 评论 0原文

我正在编写一个 C# 应用程序,它必须使用客户提供的 C++ api。当 vb6 应用程序引用该库时,该库工作正常,但是当我在 c# 应用程序中引用它并尝试调用相同的方法时,我会得到不同(错误)的行为。我调用的方法采用几个字符串参数。如果我没有该库的源代码,我只能猜测可能出了什么问题,这使我产生以下想法:该库是否可能被设计为仅从 vb6 调用?我的意思是,例如,它可能期望字符串参数以某种不同于 c# 使用的方式进行编码。如果是这样,有什么解决方法吗?到目前为止,我能做的最好的事情就是创建一个 vb6 包装器 ocx,但这不是任何优雅且最不易于部署的解决方案。

我正在发布初始化对象的代码:


        ApiPrnClass apiprn;      // this is the class imported form the com reference
        for (int j = 0; j < 10; j++)
        {
            apiprn = new ApiPrnClass();  
            apiprn.FMGetModel(_TIPODISPOSITIVO.iDocument);
            apiprn.FMPRNFormat(_TIPODISPOSITIVO.iDocument, _TIPOFORMATO.DEL_CONDENSED, "");
            apiprn.PRNBeforePrint(_TIPODISPOSITIVO.iDocument, "");

            for (int i = 0; i < 10; i++)
            {
                
                string linea = "TEST C/ BUFF XXX-----------------------".Replace("XXX", (10 * j + i).ToString().PadLeft(3, '0'));
                apiprn.FMPrint(_TIPODISPOSITIVO.iDocument, linea);
            }

            
            apiprn.PRNAfterPrint(_TIPODISPOSITIVO.iDocument);
            System.Threading.Thread.Sleep(1000);
        }

我将不胜感激任何帮助, 谢谢, 贝尔纳贝

I'm writing a C# application that has to consume a C++ api provided by my customer. The library works fine when it's referenced by a vb6 application, but when I reference it in my c# application and try to call the same methods, I get a different (wrong) behavior. The methods I'm calling take a couple of string arguments. Provided that I don't have the library's source code, I can only guess what could be wrong and this leads me to the following thought: Is it possible that the library could have been designed to be called from vb6 only? I mean for example, that it could be expecting the string parameters to be encoded in a certain way different from the one c# uses. If so, is there any workaround for this? So far the best I could do was to create a vb6 wrapper ocx, but it's not any elegant and least of all easy to deploy solution.

I'm posting the code which initializes the object:


        ApiPrnClass apiprn;      // this is the class imported form the com reference
        for (int j = 0; j < 10; j++)
        {
            apiprn = new ApiPrnClass();  
            apiprn.FMGetModel(_TIPODISPOSITIVO.iDocument);
            apiprn.FMPRNFormat(_TIPODISPOSITIVO.iDocument, _TIPOFORMATO.DEL_CONDENSED, "");
            apiprn.PRNBeforePrint(_TIPODISPOSITIVO.iDocument, "");

            for (int i = 0; i < 10; i++)
            {
                
                string linea = "TEST C/ BUFF XXX-----------------------".Replace("XXX", (10 * j + i).ToString().PadLeft(3, '0'));
                apiprn.FMPrint(_TIPODISPOSITIVO.iDocument, linea);
            }

            
            apiprn.PRNAfterPrint(_TIPODISPOSITIVO.iDocument);
            System.Threading.Thread.Sleep(1000);
        }

I would appreciate any help,
Thanks,
Bernabé

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半窗疏影 2024-08-27 20:44:04

我简直不敢相信问题的原因,我自己找到了。我意识到,当从 ac# 控制台应用程序调用时,该库运行良好,但从 winforms 使用时出错(之前没有提到,它是一个旨在打印票证的库)。我知道这两种类型的应用程序之间可能存在的唯一区别是控制台应用程序运行一个专用于运行代码的线程,而 winforms 则检查事件队列。看来这个库的代码是如此扭曲,以至于它无法支持这种切换。所以我尝试从 Windows 应用程序调用该库,但在另一个线程中......并且它有效!

不管怎样,谢谢大家的回答
贝尔纳贝

I can't believe the cause of the problem, I found it myself. I realized that the library was doing fine when called from a c# console application but wrong when used from a winforms (didn't mention it before, it was a library intended to print a ticket). The only difference i knew there could be between the two types of application is that the console application runs a thread dedicated to the runing code while the winforms goes to check the event qeue. It seems the library was so twisted-code that it couldn't support that switching. So I tryied calling the library from the windows application but in another thread....and it worked!

Thanks for all the answers anyway,
Bernabé

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文