C ++调用转换 [英] C++ Call conversion
问题描述
我是VB.net程序员,必须使用第三方Borland C ++ dll。
我们有一个成功的VC ++提供许多函数的包装器
可以在VB.net中声明和调用
我现在想将包装器翻译成C#。
我可以在(原谅双关语)基本时尚中找到我的C#。
我想翻译下面的主程序。
我正处于模拟从LoadLibrary获取指针的第一阶段。
我已成功使用:
[DllImport(" KERNEL32.DLL") ]
公共静态extern long LoadLibrary(字符串lpLibFileName);
当我给它输入dll名称时它传回了大量的数据
当我输入一个不存在的名字时,
传回了一个较小的数字。
但是当传递一个不存在的名字时,下面的C ++代码会返回空值
所以我想我必须像C ++一样使用HINSTANCE。
这导致我使用模块和marshal.getHinstance
但是我看不出如何将模块分配给我感兴趣的dll。
公共长bcLoadLibrary(字符串库)
{
long lngResult;
模块m;
//m.Name=Library; / READONLY !!
IntPtr p = new IntPtr(0);
p = Marshal.GetHINSTANCE(m);
lngResult =(长)p;
返回lngResult;
}
我是否接近或深入杂草?
谢谢
Bob
C ++代码片段是
HINSTANCE pScadaDll;
TfAttach dllAttach;
TsSimAPIFuncs * pMainDllRec = NULL;
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID
lpReserved)
{
DWORD dwResult = 0;
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH :
pScadaDll = LoadLibrary(" TheDLL.dll");
if(pScadaDll!= NULL)
{
char sMsg [500];
wsprintf(sMsg,附加的TheDLL.dll结果:);
MessageBox(NULL,sMsg," Success" ;,MB_OK);
dllAttach =(TfAttach)GetProcAddress(pScadaDll," _dll_Attach");
>
推荐答案
Bob,
Bob,
我已成功使用:
[DllImport(" KERNEL32.DLL" )]
公共静态extern long LoadLibrary(字符串lpLibFileName);
当我给它输入dll名称时它传回了大量的数据当它我传回一个较小的数字时喂了一个不存在的名字。
您应该使用IntPtr作为返回类型,不要太久。
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID > lpReserved)
{
DWORD dwResult = 0;
switch(ul_reason_for_call)
{case} DLL_PROCESS_ATTACH:
pScadaDll = LoadLibrary(" TheDLL.dll" ;);
I have successfully used:
[DllImport ("KERNEL32.DLL")]
public static extern long LoadLibrary(string lpLibFileName);
This passed back a large number when I fed it the dll name and it
passed back a smaller number when I fed a nonexistent name.
You should use IntPtr as the return type, not long.
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
DWORD dwResult = 0;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
pScadaDll = LoadLibrary("TheDLL.dll");
DllMain文档明确表示你不能在DllMain中调用LoadLibrary
。
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
The DllMain docs says explicitly that you must not call LoadLibrary
from within DllMain.
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
您好bclegg,
感谢您在社区发帖!
根据我的理解,你可以在
C#中遇到P / invoke LoadLibrary的问题。
= ===============================
实际上,LoadLibrary的返回类型是HMODULE,在.Net你
应该把它作为IntPtr编组。因此,您应将其声明为:
[DllImport(" KERNEL32.DLL")]
public static extern IntPtr LoadLibrary(string lpLibFileName);
但是,使用long作为返回值不会导致严重问题。(因为
IntPtr内部也是一个long类型)
使用LoadLibrary和一个不存在的文件名,返回值应该是
null,在IntPtr中应该是0。(我已经为你测试了这个),所以我可以
不明白为什么你的返回值只是一个较小的数字。是
小数字是什么? 0?
此外,您主要关注的是什么?
祝您好运,
Jeffrey Tan
Microsoft在线合作伙伴支持
安全! - www.microsoft.com/security
此帖子按原样提供没有保证,也没有赋予任何权利。
Hi bclegg,
Thank you for posting in the community!
Based on my understanding, you meet the problem of P/invoke LoadLibrary in
C#.
================================
Actually, the return type of LoadLibrary is HMODULE, which in .Net you
should marshal it as IntPtr. So you should declare it as:
[DllImport ("KERNEL32.DLL")]
public static extern IntPtr LoadLibrary(string lpLibFileName);
But, use long as the return value will not cause serious problem.(Because
IntPtr is also a long type internally)
Use LoadLibrary with a nonexisted file name, the return value should be
null, which should be 0 in IntPtr.(I have tested this for you), so I can
not understand why your return value is only a "smaller number". Is the
"small number" 0?
Also, where is the main concern of you?
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Hello Mattias,
感谢您的回复。
冒着继续讨论线索的风险,
你能否告知pScadaDll = LoadLibrary(TheDLL.dll);
应放在哪里。
即可接受在此时进行子程序调用
或者C ++ DLL是否应该向界面提供连接功能。
问候
Bob
Mattias Sj?gren写道:
Hello Mattias,
Thank you for your reply.
At the risk of continuing this discussion offthread,
Could you please advise where the pScadaDll = LoadLibrary("TheDLL.dll");
should be placed.
ie Is acceptable to make a subroutine call at this point which does it
or should the C++ DLL present a ''Connect'' function to the interface.
Regards
Bob
Mattias Sj?gren wrote:
鲍勃,
Bob,
我已成功使用:
[DllImport(" KERNEL32.DLL")]
public static extern long LoadLibrary(string lpLibFileName);
当我输入dll名称时,它传回了大量数字,当我输入一个不存在的名字时,它传回了一个较小的数字。
I have successfully used:
[DllImport ("KERNEL32.DLL")]
public static extern long LoadLibrary(string lpLibFileName);
This passed back a large number when I fed it the dll name and it
passed back a smaller number when I fed a nonexistent name.
你应该使用IntPtr作为回报类型,不是很长。
You should use IntPtr as the return type, not long.
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID
lpReserved)
{
DWORD dwResult = 0;
切换(ul_reason_for_call)
{
案例DLL_PROCESS_ATTACH:
pScadaDll = LoadLibrary(" TheDLL.dll");
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
DWORD dwResult = 0;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
pScadaDll = LoadLibrary("TheDLL.dll");
DllMain文档明确说明了你不得在DllMain内调用LoadLibrary
。
Mattias
The DllMain docs says explicitly that you must not call LoadLibrary
from within DllMain.
Mattias
这篇关于C ++调用转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!