从托管C ++调用Legacy C函数 [英] Calling Legacy C function from Managed C++

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

问题描述

请原谅我,如果我的帖子看起来有点业余......


我要求你们中的一些聪明人帮助

获取符合esa.h中的规范的托管调用write

for Esa_Init。当我打电话时,VS2005报告

AccessViolationException并引用第4个参数

(EsaT_State_Handle)。我不知道如何定义或将此

引用正确传递给旧DLL。我究竟做错了什么?谢谢

提前!!

esa.h

****************** ****************

#define SIM_EXPORT DLLIMPORT


typedef struct EsaT_State EsaT_State;

typedef EsaT_State * EsaT_State_Handle;


typedef enum EsaT_Compcode

{

EsaC_Comp_Failure,

EsaC_Comp_Success

} EsaT_Compcode;


DLLIMPORT EsaT_Compcode Esa_Init(int argc,char * argv [],int options,

EsaT_State_Handle * esa_state_pptr);

************************************
DLL中的CPP包装器:

****************************

#include" stdafx.h" //包括上面的esa.h


// Globals

EsaT_State_Handle esaHandle;


externC __declspec(dllexport)int __stdcall main(int argc,char *

argv [])

{

Esa_Init(argc,argv,ESAC_OPTS_NONE, & esaHandle);

返回0;

}

***************** ************

Forgive me if my post seems a little amateurish...

I''m requesting assistance from some of you smart folks out there to
get the managed calls write that meet the specification in the esa.h
for Esa_Init. When I make a call, VS2005 reports
"AccessViolationException" and refers to the 4th parameter
(EsaT_State_Handle). I don''t know how to define nor pass this
reference correctly to the legacy DLL. What am I doing wrong? Thanks
in advance!!
esa.h
**********************************
#define SIM_EXPORT DLLIMPORT

typedef struct EsaT_State EsaT_State;
typedef EsaT_State * EsaT_State_Handle;

typedef enum EsaT_Compcode
{
EsaC_Comp_Failure,
EsaC_Comp_Success
} EsaT_Compcode;

DLLIMPORT EsaT_Compcode Esa_Init (int argc, char *argv[], int options,
EsaT_State_Handle * esa_state_pptr);
************************************
CPP Wrapper in DLL:
****************************
#include "stdafx.h" //Includes esa.h above

//Globals
EsaT_State_Handle esaHandle;

extern "C" __declspec(dllexport) int __stdcall main(int argc, char*
argv[])
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************

推荐答案

我要求你们中的一些聪明人帮忙那里到
I''m requesting assistance from some of you smart folks out there to

获得符合esa.h中规范的托管调用写入

for Esa_Init。当我打电话时,VS2005报告

AccessViolationException并引用第4个参数

(EsaT_State_Handle)。我不知道如何定义或将此

引用正确传递给旧DLL。我究竟做错了什么?谢谢

提前!!
get the managed calls write that meet the specification in the esa.h
for Esa_Init. When I make a call, VS2005 reports
"AccessViolationException" and refers to the 4th parameter
(EsaT_State_Handle). I don''t know how to define nor pass this
reference correctly to the legacy DLL. What am I doing wrong? Thanks
in advance!!



该函数的文档说什么?

您是否传递了正确的值?

访问权限违规意味着该功能正在读取或写入无法访问的
地方。由于错误的指针值可以确定。

What does the documentation for that function say?
Are you passing the correct values?
An access violation means that that function is reading or writing in a
place that it has no access to. Probebly due to bad pointer values.


esa.h

**************** ******************

#define SIM_EXPORT DLLIMPORT


typedef struct EsaT_State EsaT_State;

typedef EsaT_State * EsaT_State_Handle;


typedef enum EsaT_Compcode

{

EsaC_Comp_Failure,

EsaC_Comp_Success

} EsaT_Compcode;


DLLIMPORT EsaT_Compcode Esa_Init(int argc,char * argv [],int options,

EsaT_State_Handle * esa_state_pptr);

************************************ br />

DLL中的CPP包装器:

************************** **

#include" stdafx.h" //包括上面的esa.h


// Globals

EsaT_State_Handle esaHandle;


externC __declspec(dllexport)int __stdcall main(int argc,char *

argv [])

{

Esa_Init(argc,argv,ESAC_OPTS_NONE, & esaHandle);

返回0;

}

***************** ************
esa.h
**********************************
#define SIM_EXPORT DLLIMPORT

typedef struct EsaT_State EsaT_State;
typedef EsaT_State * EsaT_State_Handle;

typedef enum EsaT_Compcode
{
EsaC_Comp_Failure,
EsaC_Comp_Success
} EsaT_Compcode;

DLLIMPORT EsaT_Compcode Esa_Init (int argc, char *argv[], int options,
EsaT_State_Handle * esa_state_pptr);
************************************
CPP Wrapper in DLL:
****************************
#include "stdafx.h" //Includes esa.h above

//Globals
EsaT_State_Handle esaHandle;

extern "C" __declspec(dllexport) int __stdcall main(int argc, char*
argv[])
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************



为什么要在这里输出main?

main是你的入口点申请。

Esa_init在argc和argv中的期望是什么?

esaHandle是否需要是有效的指针值?


- -

亲切的问候,

Bruno van Dooren MVP - VC ++
http://msmvps.com/blogs/vanDooren
br ********************** @ hotmail.com




< br ************** @ cox.netwrote in message

news: 11*********************@j4g2000prf.googlegrou ps.com ...

<br**************@cox.netwrote in message
news:11*********************@j4g2000prf.googlegrou ps.com...

原谅我如果是y post似乎有点业余...


我要求你们中的一些聪明人帮忙

得到托管电话写的那个符合esa.h中的规范

for Esa_Init。当我打电话时,VS2005报告

AccessViolationException并引用第4个参数

(EsaT_State_Handle)。我不知道如何定义或将此

引用正确传递给旧DLL。我究竟做错了什么?谢谢

提前!!
Forgive me if my post seems a little amateurish...

I''m requesting assistance from some of you smart folks out there to
get the managed calls write that meet the specification in the esa.h
for Esa_Init. When I make a call, VS2005 reports
"AccessViolationException" and refers to the 4th parameter
(EsaT_State_Handle). I don''t know how to define nor pass this
reference correctly to the legacy DLL. What am I doing wrong? Thanks
in advance!!



这是我用谷歌看到的OPNET的Esa_Init吗?其他人

观察到OPNET ESA不是线程安全的。另外,在Esa_Init之前看起来Esa_Main是




但是,我认为你的间接性很差。

Is this the Esa_Init for OPNET which I see with a google? Other people
observe that OPNET ESA isn''t threadsafe. Also, it looks like Esa_Main is
called before Esa_Init.

Mostly though, I think you have bad indirection.


>


esa.h

****************** ****************

#define SIM_EXPORT DLLIMPORT


typedef struct EsaT_State EsaT_State;

typedef EsaT_State * EsaT_State_Handle;


typedef enum EsaT_Compcode

{

EsaC_Comp_Failure,

EsaC_Comp_Success

} EsaT_Compcode;
>

esa.h
**********************************
#define SIM_EXPORT DLLIMPORT

typedef struct EsaT_State EsaT_State;
typedef EsaT_State * EsaT_State_Handle;

typedef enum EsaT_Compcode
{
EsaC_Comp_Failure,
EsaC_Comp_Success
} EsaT_Compcode;



我不喜欢...类型和typedef的同名?摆脱

枚举标识符,例如typedef enum {...} EsaT_Compcode。不喜欢与上面的EsaT_State相同的


I don''t like that... same name for a type and a typedef? Get rid of the
enum identifier, like "typedef enum { ... } EsaT_Compcode". Don''t like the
same thing done with EsaT_State above either.


>

DLLIMPORT EsaT_Compcode Esa_Init(int argc,char * argv [],int options,

EsaT_State_Handle * esa_state_pptr);
>
DLLIMPORT EsaT_Compcode Esa_Init (int argc, char *argv[], int options,
EsaT_State_Handle * esa_state_pptr);



没有指定调用约定......你应该这样做。

Calling convention isn''t specified... you should do that.


*** *********************************


DLL中的CPP Wrapper:

****************************

#include" stdafx。 H" //包括上面的esa.h


// Globals

EsaT_State_Handle esaHandle;


externC __declspec(dllexport)int __stdcall main(int argc,char *

argv [])
************************************
CPP Wrapper in DLL:
****************************
#include "stdafx.h" //Includes esa.h above

//Globals
EsaT_State_Handle esaHandle;

extern "C" __declspec(dllexport) int __stdcall main(int argc, char*
argv[])



为什么主要从dll导出?这可能不是你想要的。

Why is main exported from a dll? That''s probably not what you intended.


{

Esa_Init(argc,argv,ESAC_OPTS_NONE,& esaHandle);

返回0;

}

************************ *****
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************



是 - OPNET ESA。只是不知道另一种更好的方法来控制模仿过程中的b $ b。请原谅我 - VC不是我的一杯茶。

有没有办法包装使线程安全的代码?我通过调用约定来假设

,你的意思是__stdcall(不知道我为什么离开它

out)...至于type / typedef decl, OPNET使用了约定

- 我显然不太了解这个结构。我的目标

是能够从C ++调用C API。我对

语言的无知使我无法正确接听电话 - 或许我是

正如你所说的那样,线程问题正在导致受保护的

内存问题。那么,还有其他什么建议吗?


我感谢你的帮助 - 这既是一次痛苦又有益的经历!



6月14日晚上8:39,Ben Voigt [C ++ MVP]" < r ... @nospam.nospamwrote:
Yes--OPNET ESA. Just didn''t know of another better way to "control"
the simulation process. Please forgive me--VC is not my cup of tea.
Is there any way to "wrap" the code to make it thread safe? I assume
by calling convention, you mean __stdcall (don''t know why I left it
out)... As far as the type/typedef decl, the convention was used
by OPNET--I obviously don''t understand the construct enough. My goal
is to be able to call the C API from C++. My ignorance of both
languages is preventing me from getting the call right--or maybe I am
and just as you said, threading issues are causing the protected
memory issue. So, any other suggestions?

I appreciate your help--this has been both a painful and rewarding
experience!

On Jun 14, 8:39 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:

< briankirkpatr ... @ cox.netwrote in message


新闻:11 ********************* @ j4g2000prf.googlegrou ps.com ...
<briankirkpatr...@cox.netwrote in message

news:11*********************@j4g2000prf.googlegrou ps.com...

请原谅我,如果我的帖子看起来有点业余......
Forgive me if my post seems a little amateurish...


我要求你们中的一些聪明人帮助

获取符合esa.h中的规范的托管调用write

for Esa_Init。当我打电话时,VS2005报告

AccessViolationException并引用第4个参数

(EsaT_State_Handle)。我不知道如何定义或将此

引用正确传递给旧DLL。我究竟做错了什么?谢谢

提前!!
I''m requesting assistance from some of you smart folks out there to
get the managed calls write that meet the specification in the esa.h
for Esa_Init. When I make a call, VS2005 reports
"AccessViolationException" and refers to the 4th parameter
(EsaT_State_Handle). I don''t know how to define nor pass this
reference correctly to the legacy DLL. What am I doing wrong? Thanks
in advance!!



这是我用谷歌看到的OPNET的Esa_Init吗?其他人

观察到OPNET ESA不是线程安全的。另外,在Esa_Init之前看起来Esa_Main是




但是,我认为你的间接性很差。


Is this the Esa_Init for OPNET which I see with a google? Other people
observe that OPNET ESA isn''t threadsafe. Also, it looks like Esa_Main is
called before Esa_Init.

Mostly though, I think you have bad indirection.


esa.h

******************************** **

#define SIM_EXPORT DLLIMPORT
esa.h
**********************************
#define SIM_EXPORT DLLIMPORT


typedef struct EsaT_State EsaT_State;

typedef EsaT_State * EsaT_State_Handle;
typedef struct EsaT_State EsaT_State;
typedef EsaT_State * EsaT_State_Handle;


typedef enum EsaT_Compcode

{

EsaC_Comp_Failure,

EsaC_Comp_Success

} EsaT_Compcode;
typedef enum EsaT_Compcode
{
EsaC_Comp_Failure,
EsaC_Comp_Success
} EsaT_Compcode;



我不喜欢...类型和typedef的同名?摆脱

枚举标识符,例如typedef enum {...} EsaT_Compcode。不喜欢与上面的EsaT_State相同的



I don''t like that... same name for a type and a typedef? Get rid of the
enum identifier, like "typedef enum { ... } EsaT_Compcode". Don''t like the
same thing done with EsaT_State above either.


DLLIMPORT EsaT_Compcode Esa_Init(int argc,char * argv [] ,int options,

EsaT_State_Handle * esa_state_pptr);
DLLIMPORT EsaT_Compcode Esa_Init (int argc, char *argv[], int options,
EsaT_State_Handle * esa_state_pptr);



没有指定调用约定......你应该这样做。


Calling convention isn''t specified... you should do that.


*** *********************************
************************************


CP中的CPP包装器:

****************************

#include" stdafx.h" //包括esa.h以上
CPP Wrapper in DLL:
****************************
#include "stdafx.h" //Includes esa.h above


// Globals

EsaT_State_Handle esaHandle;
//Globals
EsaT_State_Handle esaHandle;


extern" C" __declspec(dllexport)int __stdcall main(int argc,char *

argv [])
extern "C" __declspec(dllexport) int __stdcall main(int argc, char*
argv[])



为什么主要从dll导出?这可能不是你想要的。


Why is main exported from a dll? That''s probably not what you intended.


{

Esa_Init(argc,argv,ESAC_OPTS_NONE,& esaHandle);

返回0;

}

************************ ***** - 隐藏引用的文字 -
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************- Hide quoted text -



- 显示引用的文字 -


- Show quoted text -



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

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