从非托管 C 调用托管代码 [英] Calling Managed Code From Unmanaged C

查看:19
本文介绍了从非托管 C 调用托管代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在大约一天半之后,我的进展为零.

So after about a day and a half with this I've made zero progress.

我需要用 C 语言编写一个用于现有应用程序插件的 DLL.DLL 必须由 Visual Studio 2008 编译器使用以下选项进行编译

I need to write a DLL in C that is used a plugin for an existing application. The DLL has to be compiled by the Visual Studio 2008 compiler with the following options

cl -DNT40 -DPOMDLL -DCRTAPI1=_cdecl -DCRTAPI2=cdecl -D_WIN32 -DWIN32 -DWIN32_LEAN_AND_MEAN -DWNT -DBYPASS_FLEX -D_INTEL=1 -DIPLIB=none -I.-I"C:plm2T-RACTcExpress53include" -I"C:Program Files (x86)Microsoft Visual Studio 8VCinclude"-c -nologo -EHsc -W1 -Ox -Oy- -MD C:mydirmyDll.c

cl -DNT40 -DPOMDLL -DCRTAPI1=_cdecl -DCRTAPI2=cdecl -D_WIN32 -DWIN32 -DWIN32_LEA N_AND_MEAN -DWNT -DBYPASS_FLEX -D_INTEL=1 -DIPLIB=none -I. -I"C:plm2T-RACTcEx press53include" -I"C:Program Files (x86)Microsoft Visual Studio 8VCinclude" -c -nologo -EHsc -W1 -Ox -Oy- -MD C:mydirmyDll.c

然后将其链接到应用程序库.

It's then linked to applications library's.

它实际上需要做什么以及导致我出现问题的原因是,在其中一种方法中,它需要从 sql server 2008 r2 提取数据.

What it actually needs to do and what's causing me the issues is that within one of the methods it needs to pull data from a sql server 2008 r2.

根据我今天所见,您无法直接在 C 中执行此操作,因为 2005 年以后的 SQL Server 旨在与 CLR 语言(C#、C++、VB.Net)进行通信.

From what I've seen today you wouldn't be able to do this directly in C as SQL Server past 2005 is designed to communicate with CLR languages( C#, C++, VB.Net).

建议我使用 VB.Net dll 处理所有数据库通信,然后从 C dll 中调用所述函数.

It was suggested that I handle all the database communication with a VB.Net dll and then call said function from within the C dll.

关于从非托管代码调用托管 dll 的主题,我发现的大部分内容都谈到了 pinvoke 或 com 包装器,并且主要是从 c++ 的角度.

Most of what I've found on the topic of calling managed dll's from unmanaged code has talked about pinvoke or com wrappers and mainly from the perspective of c++.

我对 COM 或任何提到的技术的经验为零,因此如果有人能提供帮助,将不胜感激.

I have zero experience with COM or any mentioned techniques so if anyone could help out it would be much appreciated.

推荐答案

我解决这个问题的方法是简单地让 C dll 调用 cmd 并执行编译的 VB.NET 可执行文件并通过管道输出它.虽然这不是一个完全理想的解决方案,但当我只需要一个字符串时,它可以在这种情况下工作.

The way I resolved this was simply having the C dll call cmd and execute a compile VB.NET executable and pipe it's output. Although this isn't wasn't a totally ideal solution it worked in this situation when all I required back was a single string.

这篇关于从非托管 C 调用托管代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆