473,385 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Calling Legacy C function from Managed C++

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;
}
*****************************

Jun 14 '07 #1
11 3159
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!!
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);
************************************
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;
}
*****************************
Why do you export main here?
main is the entry point of your application.
What does Esa_init expect in argc and argv?
Does esaHandle need to be a valid pointer value?

--
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
br**********************@hotmail.com
Jun 14 '07 #2

<br**************@cox.netwrote in message
news:11*********************@j4g2000prf.googlegrou ps.com...
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!!
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;
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);
Calling convention isn't specified... you should do that.
************************************
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[])
Why is main exported from a dll? That's probably not what you intended.
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************

Jun 15 '07 #3
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

news:11*********************@j4g2000prf.googlegrou ps.com...
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!!

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;

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);

Calling convention isn't specified... you should do that.
************************************
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[])

Why is main exported from a dll? That's probably not what you intended.
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************- Hide quoted text -

- Show quoted text -

Jun 15 '07 #4
I'm not able to pass the 4th parameter correctly. If I make the DLL
an Application, with the simulation model passed in the command line,
the same function call to Esa_Init() works correctly.

The argc and argv are the standard command line parameter count and
parameter array values. The esaHandle is an "opaque" reference--I
understand that it allows provides a handle to the simulation to be
passed to other functions called within OPNET ESA API.

So... If this is a threading issue (AccessViolationException) then
how do I make the call in a thread-safe manner?

DOCUMENTATION:
Esa_Init (argc, argv, options, esa_state_pptr)

Argument Type Description
argc int size of argv array
argv char ** array of strings, typically as
provided to the main() entry point
options int one of:
ESAC_INIT_OPTS_TRACE-trace all subsequent ESA calls (similar to the
tracing of kernel procedures in ODB)
ESAC_INIT_OPTS_DEBUG-run the simulation with the ODB interface
ESAC_OPTS_NONE-no special options
esa_state_pptr EsaT_State_Handle * will be filled with an ESA
opaque data structure used by the other ESA APIs.

Return Type
EsaT_Compcode -- EsaC_Comp_Success if initialization succeeds;
EsaC_Comp_Failure otherwise.

Details
This function performs the main initialization of the ESA library. It
sets up the OPNET analysis software environment based on the argument
list, loads preference files (env_db, .ef), and so on.

On Jun 14, 4:00 pm, "Bruno van Dooren [MVP - VC++]"
<bruno_nos_pam_van_doo...@hotmail.comwrote:
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!!

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);
************************************
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;
}
*****************************

Why do you export main here?
main is the entry point of your application.
What does Esa_init expect in argc and argv?
Does esaHandle need to be a valid pointer value?

--
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
bruno_nos_pam_van_doo...@hotmail.com- Hide quoted text -

- Show quoted text -

Jun 15 '07 #5

"kirkpabk" <br*******************@langley.af.milwrote in message
news:11*********************@w5g2000hsg.googlegrou ps.com...
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
You just have to make sure you don't call from multiple threads at once.
That might mean you are calling from the wrong place to begin with, or it
might mean you need a critical section, mutex, or other lock.
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
Don't worry about that.
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?
What is the actual name of the function calling Esa_Init, I assume it isn't
main when you are using a DLL? And where does that get called from? If you
are using DllMain, for example, make sure you are checking the dwReason
argument and understand what each of the four reasons means (PROCESS_ATTACH,
THREAD_ATTACH, PROCESS_DETACH, THREAD_DETACH).
>
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

news:11*********************@j4g2000prf.googlegro ups.com...
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!!

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;

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);

Calling convention isn't specified... you should do that.
************************************
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[])

Why is main exported from a dll? That's probably not what you intended.
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************- Hide quoted text -

- Show quoted text -

Jun 15 '07 #6
Ben,

I'm sure there is some snickering in the background because of my
ignorance <grin>. Yes, in the DLL I do have the call to Esa_Init in
the main(). I converted from an EXE to DLL--so because of my
ignorance (again), I didn't know enough to make it a DllMain (what
does that do for me?). I am calling the DLL main function from a C#
application. When I execute the function within it, I get the
"Unhandled exeception at 0x7cblah (msvcr71.dll) in giss_clr.exe:
0xC00blah: Unwind exception code." <Another problem--not necessarily
related to the original problem>. Meanwhile, I'll look into the
dwReason elements you made reference to.

Again thanks for lending your expertise!
v/r
Brian

On Jun 15, 1:36 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"kirkpabk" <brian.kirkpatrick....@langley.af.milwrote in message

news:11*********************@w5g2000hsg.googlegrou ps.com...
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

You just have to make sure you don't call from multiple threads at once.
That might mean you are calling from the wrong place to begin with, or it
might mean you need a critical section, mutex, or other lock.
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

Don't worry about that.
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?

What is the actual name of the function calling Esa_Init, I assume it isn't
main when you are using a DLL? And where does that get called from? If you
are using DllMain, for example, make sure you are checking the dwReason
argument and understand what each of the four reasons means (PROCESS_ATTACH,
THREAD_ATTACH, PROCESS_DETACH, THREAD_DETACH).


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
>news:11*********************@j4g2000prf.googlegro ups.com...
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!!
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;
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);
Calling convention isn't specified... you should do that.
************************************
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[])
Why is main exported from a dll? That's probably not what you intended.
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -

Jun 18 '07 #7

"kirkpabk" <br*******************@langley.af.milwrote in message
news:11**********************@c77g2000hse.googlegr oups.com...
Ben,

I'm sure there is some snickering in the background because of my
None whatsoever. I'm just trying to figure out what's going on.
ignorance <grin>. Yes, in the DLL I do have the call to Esa_Init in
the main(). I converted from an EXE to DLL--so because of my
ignorance (again), I didn't know enough to make it a DllMain (what
does that do for me?). I am calling the DLL main function from a C#
The name "main" is C is reserved for the application entry point. I
wouldn't export it from a DLL, and I wouldn't use it for a function invoked
from user code.
application. When I execute the function within it, I get the
"Unhandled exeception at 0x7cblah (msvcr71.dll) in giss_clr.exe:
0xC00blah: Unwind exception code." <Another problem--not necessarily
related to the original problem>. Meanwhile, I'll look into the
dwReason elements you made reference to.
Can you start by renaming your function to something other than "main"?
>
Again thanks for lending your expertise!
v/r
Brian

On Jun 15, 1:36 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>"kirkpabk" <brian.kirkpatrick....@langley.af.milwrote in message

news:11*********************@w5g2000hsg.googlegro ups.com...
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

You just have to make sure you don't call from multiple threads at once.
That might mean you are calling from the wrong place to begin with, or it
might mean you need a critical section, mutex, or other lock.
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

Don't worry about that.
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?

What is the actual name of the function calling Esa_Init, I assume it
isn't
main when you are using a DLL? And where does that get called from? If
you
are using DllMain, for example, make sure you are checking the dwReason
argument and understand what each of the four reasons means
(PROCESS_ATTACH,
THREAD_ATTACH, PROCESS_DETACH, THREAD_DETACH).


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
>>news:11*********************@j4g2000prf.googlegr oups.com...
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!!
>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;
>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);
>Calling convention isn't specified... you should do that.
************************************
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[])
>Why is main exported from a dll? That's probably not what you
intended.
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************- Hide quoted text -
>- Show quoted text -- Hide quoted text -

- Show quoted text -


Jun 18 '07 #8

"kirkpabk" <br*******************@langley.af.milwrote in message
news:11**********************@c77g2000hse.googlegr oups.com...
Ben,

I'm sure there is some snickering in the background because of my
ignorance <grin>. Yes, in the DLL I do have the call to Esa_Init in
the main(). I converted from an EXE to DLL--so because of my
ignorance (again), I didn't know enough to make it a DllMain (what
does that do for me?). I am calling the DLL main function from a C#
application. When I execute the function within it, I get the
"Unhandled exeception at 0x7cblah (msvcr71.dll) in giss_clr.exe:
0xC00blah: Unwind exception code." <Another problem--not necessarily
related to the original problem>. Meanwhile, I'll look into the
dwReason elements you made reference to.

Again thanks for lending your expertise!
Also, I'm trying to figure out where the "Managed C++" comes in... You
wouldn't be using DLL exports to call a C++/CLI (or even Managed Extensions
for C++ which is now dead) function from C#. C++/CLI makes .NET types that
you use the same as any Microsoft class, by adding a reference to the
assembly.
Jun 18 '07 #9
On Jun 18, 2:28 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"kirkpabk" <brian.kirkpatrick....@langley.af.milwrote in message

news:11**********************@c77g2000hse.googlegr oups.com...
Ben,
I'm sure there is some snickering in the background because of my

None whatsoever. I'm just trying to figure out what's going on.
ignorance <grin>. Yes, in the DLL I do have the call to Esa_Init in
the main(). I converted from an EXE to DLL--so because of my
ignorance (again), I didn't know enough to make it a DllMain (what
does that do for me?). I am calling the DLL main function from a C#

The name "main" is C is reserved for the application entry point. I
wouldn't export it from a DLL, and I wouldn't use it for a function invoked
from user code.
application. When I execute the function within it, I get the
"Unhandled exeception at 0x7cblah (msvcr71.dll) in giss_clr.exe:
0xC00blah: Unwind exception code." <Another problem--not necessarily
related to the original problem>. Meanwhile, I'll look into the
dwReason elements you made reference to.

Can you start by renaming your function to something other than "main"?


Again thanks for lending your expertise!
v/r
Brian
On Jun 15, 1:36 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"kirkpabk" <brian.kirkpatrick....@langley.af.milwrote in message
>news:11*********************@w5g2000hsg.googlegro ups.com...
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
You just have to make sure you don't call from multiple threads at once.
That might mean you are calling from the wrong place to begin with, or it
might mean you need a critical section, mutex, or other lock.
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
Don't worry about that.
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?
What is the actual name of the function calling Esa_Init, I assume it
isn't
main when you are using a DLL? And where does that get called from? If
you
are using DllMain, for example, make sure you are checking the dwReason
argument and understand what each of the four reasons means
(PROCESS_ATTACH,
THREAD_ATTACH, PROCESS_DETACH, THREAD_DETACH).
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
>news:11*********************@j4g2000prf.googlegro ups.com...
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!!
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;
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);
Calling convention isn't specified... you should do that.
************************************
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[])
Why is main exported from a dll? That's probably not what you
intended.
{
Esa_Init(argc, argv, ESAC_OPTS_NONE, &esaHandle);
return 0;
}
*****************************- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
Could this be the typical DLL initialization issue when using
conventional DLL with CLR?
Do you have static variables in the DLL? If you do, you will have to
make the DLL gets initialized before being called from CLR envronment
(like C#).

John

Jun 19 '07 #10
Hello! Good Site! Thanks you! grmoknpnghny
Jun 21 '07 #11
Hello! Good Site! Thanks you! clseaigulqvg
Aug 16 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sai Kit Tong | last post by:
Hi, I am developing a new application running on Windows platform that needs to interface with existing legacy code - written in basic C / C++. I am trying to evaluate Java vs C#...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
2
by: Mark Olbert | last post by:
First off, the sympathy is for all you poor buggers out there who have to figure out how to marry Managed Extensions for C++ onto your legacy code. My condolences; my brief experience with the...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
1
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from...
1
by: Arne Adams | last post by:
Hi, I try to use a C# Dialog in a legacy MFC application. The problem seems to boil down to the following: from an unmanaged console application I can call any function of the managed bridge - if...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
6
by: sasha | last post by:
I have a c++ code that callls csharp. Now I want to be able to pass a function pointer from C++ to Csharp code and have c# callback to it. Is it possible and how? Here is what I have so far :...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.