473,770 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;

typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} EsaT_Compcode;

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

//Globals
EsaT_State_Hand le esaHandle;

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

Jun 14 '07 #1
11 3200
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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;

typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} EsaT_Compcode;

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

//Globals
EsaT_State_Hand le esaHandle;

extern "C" __declspec(dlle xport) 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************* *********@hotma il.com
Jun 14 '07 #2

<br************ **@cox.netwrote in message
news:11******** *************@j 4g2000prf.googl egroups.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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;

typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} 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_Hand le * 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_Hand le esaHandle;

extern "C" __declspec(dlle xport) 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.no spamwrote:
<briankirkpatr. ..@cox.netwrote in message

news:11******** *************@j 4g2000prf.googl egroups.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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;
typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} 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_Hand le * 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_Hand le esaHandle;
extern "C" __declspec(dlle xport) 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 (AccessViolatio nException) 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_Hand le * will be filled with an ESA
opaque data structure used by the other ESA APIs.

Return Type
EsaT_Compcode -- EsaC_Comp_Succe ss if initialization succeeds;
EsaC_Comp_Failu re 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...@hotm ail.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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;
typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} EsaT_Compcode;
DLLIMPORT EsaT_Compcode Esa_Init (int argc, char *argv[], int options,
EsaT_State_Hand le * esa_state_pptr) ;
*************** *************** ******
CPP Wrapper in DLL:
*************** *************
#include "stdafx.h" //Includes esa.h above
//Globals
EsaT_State_Hand le esaHandle;
extern "C" __declspec(dlle xport) 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_v an_doo...@hotma il.com- Hide quoted text -

- Show quoted text -

Jun 15 '07 #5

"kirkpabk" <br************ *******@langley .af.milwrote in message
news:11******** *************@w 5g2000hsg.googl egroups.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.no spamwrote:
><briankirkpatr ...@cox.netwrot e in message

news:11******* **************@ j4g2000prf.goog legroups.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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;
typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} 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_Hand le * 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_Hand le esaHandle;
extern "C" __declspec(dlle xport) 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.no spamwrote:
"kirkpabk" <brian.kirkpatr ick....@langley .af.milwrote in message

news:11******** *************@w 5g2000hsg.googl egroups.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.no spamwrote:
<briankirkpatr. ..@cox.netwrote in message
>news:11******* **************@ j4g2000prf.goog legroups.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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;
typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} 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_Hand le * 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_Hand le esaHandle;
extern "C" __declspec(dlle xport) 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.goo glegroups.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.no spamwrote:
>"kirkpabk" <brian.kirkpatr ick....@langley .af.milwrote in message

news:11******* **************@ w5g2000hsg.goog legroups.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_ATTAC H,
THREAD_ATTAC H, 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.no spamwrote:
<briankirkpatr ...@cox.netwrot e in message
>>news:11****** *************** @j4g2000prf.goo glegroups.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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;
typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} 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_Hand le * 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_Hand le esaHandle;
extern "C" __declspec(dlle xport) 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.goo glegroups.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.no spamwrote:
"kirkpabk" <brian.kirkpatr ick....@langley .af.milwrote in message

news:11******** **************@ c77g2000hse.goo glegroups.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.no spamwrote:
"kirkpabk" <brian.kirkpatr ick....@langley .af.milwrote in message
>news:11******* **************@ w5g2000hsg.goog legroups.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.no spamwrote:
<briankirkpatr. ..@cox.netwrote in message
>news:11******* **************@ j4g2000prf.goog legroups.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
"AccessViolatio nException" and refers to the 4th parameter
(EsaT_State_Han dle). 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_Hand le;
typedef enum EsaT_Compcode
{
EsaC_Comp_Failu re,
EsaC_Comp_Succe ss
} 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_Hand le * 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_Hand le esaHandle;
extern "C" __declspec(dlle xport) 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

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

Similar topics

3
4022
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# implementations. Originally, I have the impression that C# should be a clear winner. I started with Java and using the guideline from the book "Java Native Interface". Though complex, the book provide details of the practical implementation and potential...
3
2378
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 response). My first reply directed me to source code migration but I didn't have the source code. The second reply mentioned about .NET interoperability (PInvoke I think) but I MENTIONED THAT I COULDN'T FIND ANY DOCUMENTATION FROM MSDN LIBRARY BASED ON...
1
2914
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 written in C#. When the app calls my unmanaged functions, they work fine. But as soon as my unmanaged functions call managed functions (in the same source file!), the app reports an "unknown exception" error.
2
1645
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 process leads me to believe the Microsoft hates all of its VC++ developers :) Now, the question. I'm getting a NullReferenceException in a __gc class that wraps functionality in a legacy C/C++ app (it's actually part of the Nero burning rom api). ...
1
2605
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, System::EventArgs * e) { MyDLLInit(MyAppDisplayFunction); }
1
3445
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 VS2003, and related techniques. I have found managed to unmanaged technique very easy in VS2005, but have not been able to build anything with unmanaged to managed calls. I have legacy (unmanaged C++) that would like to leverage the .NET...
1
2256
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 I try to do the same from my mfc application the application won't start 'SeaMain.exe': 'E:\WINDOWS\system32\mscoree.dll' geladen, Keine Symbole geladen. 'SeaMain.exe': 'E:\WINDOWS\system32\msvcr71d.dll' geladen, Symbole geladen.
7
2688
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 name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
6
8545
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 : #include "windows.h" #include <stdio.h> #import "CSDll.tlb" named_guids int main(int argc, char* argv)
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.