需要在 ANSI C 中实例化外部 .NET 对象的帮助!

发布于 2024-10-20 17:58:11 字数 1336 浏览 0 评论 0原文

我正在开发一个用低级 ANSI C 编写的开源项目(我对此无法控制)。我正在尝试将 Microsoft Office Interop 功能集成到该项目的 Windows 版本中。具体来说,我想利用这些函数来生成 Excel 工作簿。

如果这是用 C# 或 C++ 编写的,这对我来说不会是问题,因为我已经做过很多次了。但我完全不知道如何在低级 C 中做到这一点!我需要从中获取此内容的 DLL 是 Microsoft.Office.Interop.Excel.dll。在 C# 中,它会是这样的:

using Microsoft.Office.Interop.Excel;

public static class Excel
{
    private static Microsoft.Office.Interop.Excel.Application m_oExcelApp;
    private static Microsoft.Office.Interop.Excel.Workbooks m_oBooks;
    private static Microsoft.Office.Interop.Excel._Workbook m_oBook;
    private static Microsoft.Office.Interop.Excel._Worksheet m_oSheet;
    private static Microsoft.Office.Interop.Excel.Range excelRange;

    public static void MakeBook()
    {
        m_oExcelApp = new Microsoft.Office.Interop.Excel.Application();
        m_oExcelApp.Visible = false;
        m_oSheet = null;
        m_oBooks = null;
        excelRange = null;

......等等。如果我能走到这一步,我真的很高兴!但据我所知,DllImport 只允许您导入函数;即我无法弄清楚如何使用它来实例化该 DLL 中的对象(即创建 Microsoft.Office.Interop.Excel.Application() 的实例,这是创建任何使用这些库)。

请理解,我完全意识到这是一项多么艰巨的任务,并且在低级 C 中执行此操作不是我的选择!但我坚持了下来,我的截止日期有点紧迫,如果你们中有人比我更精通低级 C,并且可以帮助我移植上面的代码,我将非常感激帮助!

谢谢!

编辑:我很欣赏概念性建议,但我真正需要的是一些示例 C 代码来向我展示如何实际实现它!拜托,伙计们,必须有人能帮我解决这个问题!我不需要太多,只需要一个小例子,向我展示如何在我想做的事情的背景下做到这一点,我可以从那里处理它。

I'm working on an open source project that is written in low-level ANSI C (I have no control over this). I am trying to integrate the Microsoft Office Interop functionality into the Windows build of this project. Specifically, I would like to make use of these functions to generate Excel workbooks.

If this was in C# or C++, this wouldn't be a problem for me as I've done it numerous times. But I am completely stumped on how to do this in low-level C! The DLL I need to take this from is Microsoft.Office.Interop.Excel.dll. In C#, it would go something like this:

using Microsoft.Office.Interop.Excel;

public static class Excel
{
    private static Microsoft.Office.Interop.Excel.Application m_oExcelApp;
    private static Microsoft.Office.Interop.Excel.Workbooks m_oBooks;
    private static Microsoft.Office.Interop.Excel._Workbook m_oBook;
    private static Microsoft.Office.Interop.Excel._Worksheet m_oSheet;
    private static Microsoft.Office.Interop.Excel.Range excelRange;

    public static void MakeBook()
    {
        m_oExcelApp = new Microsoft.Office.Interop.Excel.Application();
        m_oExcelApp.Visible = false;
        m_oSheet = null;
        m_oBooks = null;
        excelRange = null;

....and so-on. I would be really happy if I could just get this far! But as far as I can tell, DllImport only allows you to import functions; i.e. I can't figure out how it would be used to instantiate an object within that DLL (i.e. creating an instance of Microsoft.Office.Interop.Excel.Application(), which is required to make any use of these libraries).

Please understand that I fully realize what a daunting task this is, and doing this in low-level C was NOT my choice! But I'm stuck with it, I'm on a somewhat tight deadline, and if any of you are more versed in low-level C than I am and can help me port the above code over, I would be EXTREMELY grateful for your help!

Thanks!

EDIT: I appreciate the conceptual advice, but what I really need is some example C code to show me how to actually make it happen! Please guys, somebody has to be able to help me with this! I don't need a lot, just a small example showing me how to do it in the context of what I'm trying to do, and I can handle it from there.

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

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

发布评论

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

评论(1

小耗子 2024-10-27 17:58:11

将对象包装为不透明句柄,然后将要在 C 中使用的方法包装为采用不透明句柄的 COM 函数可能会更容易。有关详细信息,这篇 MSDN 文章可能是一个不错的起点。

It might be easier to wrap the object as an opaque handle, and then wrap the methods you want to use in C as COM functions that take the opaque handle. For more information, this MSDN article might be a good place to start.

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