Board index » cppbuilder » Calling Function in DLL Crashes When Parameter is const char*

Calling Function in DLL Crashes When Parameter is const char*


2005-03-01 01:14:51 PM
cppbuilder47
I have a MSVC++ DLL that I have dynamically loaded and linked a function
containing a const char* parameter. When I try to call the function, the
app crashes. However, many other functions that I have linked that do not
contain const char* parameters work just fine. In fact, I've written other
apps with functions containing const char* parameters and they work. I seem
to remember that I had to do something special to make them work, but can no
longer remember. Can anyone help me?
Tom
 
 

Re:Calling Function in DLL Crashes When Parameter is const char*

"Thomas Scott" < XXXX@XXXXX.COM >wrote in message
Quote
I have a MSVC++ DLL that I have dynamically loaded and linked
a function containing a const char* parameter. When I try to call the
function, the app crashes. However, many other functions that I have
linked that do not contain const char* parameters work just fine. In
fact, I've written other apps with functions containing const char*
parameters and they work. I seem to remember that I had to do
something special to make them work, but can no longer remember.
It is hard to say without seeing the actual code, but I would guess that
either the application is not providing valid parameters to the function in
the first place, or else (more likely) the function is not declared
correctly in the application and you are thus corrupting the stack.
Gambit
 

Re:Calling Function in DLL Crashes When Parameter is const char*

Or even if the declaration seems to be correct the MSVC++ DLL is not
binary compatible.
Please take a look at this documentation
www.agner.org/assem/calling_conventions.pdf
The code generated with default compiler options and (without) type
modifiers can be completly different in MSVC and BCB.
Also it could be caused by a different alignment option or enum size
option.
Please be a bit more specific to let us to help you.
by(t)e
Hofi
On Mon, 28 Feb 2005 22:03:43 -0800, Remy Lebeau (TeamB)
< XXXX@XXXXX.COM >wrote:
Quote

"Thomas Scott" < XXXX@XXXXX.COM >wrote in message
news:4223fa42$ XXXX@XXXXX.COM ...

>I have a MSVC++ DLL that I have dynamically loaded and linked
>a function containing a const char* parameter. When I try to call the
>function, the app crashes. However, many other functions that I have
>linked that do not contain const char* parameters work just fine. In
>fact, I've written other apps with functions containing const char*
>parameters and they work. I seem to remember that I had to do
>something special to make them work, but can no longer remember.

It is hard to say without seeing the actual code, but I would guess that
either the application is not providing valid parameters to the function
in
the first place, or else (more likely) the function is not declared
correctly in the application and you are thus corrupting the stack.


Gambit


 

{smallsort}

Re:Calling Function in DLL Crashes When Parameter is const char*

First of all, every other function in the DLL works just fine. I've done
this before many times, including using functions just like this one and no
problems. However, it's been a while since I've set this sort of thing up
from scratch and the following function crashes the app. If I add another
function exactly the same, but omit the const char* parameter, it works fine
as well.
#ifdef OMEGACOREDLL_EXPORTS
#define OMEGACOREDLL_API __declspec(dllexport)
#else
#define OMEGACOREDLL_API __declspec(dllimport)
#endif
OMEGACOREDLL_API long
ResourceMgr_AddTexture( const char* someFileName, int someGroupID )
{
ocHandle handle = TextureMgr.AddFromFile( someFileName, someGroupID );
return handle.GetValue();
}
Code in BCB++:
typedef long PFNADDTEXTURE( const char*, int );
#define fnRES_AddTexture "?ResourceMgr_AddTexture@@YAJPBDH@Z"
static PFNADDTEXTURE* _AddTexture = NULL;
// link to exported functions
_AddTexture = (PFNADDTEXTURE*)GetProcAddress( dllHandle, fnRES_AddTexture );
// verify all functions linked properly
if ( !_AddTexture )
{
MessageBox( NULL, "Failed to link to AddTexture function", "", MB_OK );
}
_AddTexture( "ground.jpg", 1);
App crashes with:
Project GameCreator.exe raised exception class EAccessViolation with message
'Access violation at address 616E6E55. Read of address 616E6E55.'
The function is never actually called. It fails to execute it.
"hofi" < XXXX@XXXXX.COM >wrote in message
Quote
Or even if the declaration seems to be correct the MSVC++ DLL is not
binary compatible.
Please take a look at this documentation
www.agner.org/assem/calling_conventions.pdf
The code generated with default compiler options and (without) type
modifiers can be completly different in MSVC and BCB.
Also it could be caused by a different alignment option or enum size
option.

Please be a bit more specific to let us to help you.

by(t)e
Hofi

On Mon, 28 Feb 2005 22:03:43 -0800, Remy Lebeau (TeamB)
< XXXX@XXXXX.COM >wrote:

>
>"Thomas Scott" < XXXX@XXXXX.COM >wrote in message
>news:4223fa42$ XXXX@XXXXX.COM ...
>
>>I have a MSVC++ DLL that I have dynamically loaded and linked
>>a function containing a const char* parameter. When I try to call the
>>function, the app crashes. However, many other functions that I have
>>linked that do not contain const char* parameters work just fine. In
>>fact, I've written other apps with functions containing const char*
>>parameters and they work. I seem to remember that I had to do
>>something special to make them work, but can no longer remember.
>
>It is hard to say without seeing the actual code, but I would guess that
>either the application is not providing valid parameters to the function
>in
>the first place, or else (more likely) the function is not declared
>correctly in the application and you are thus corrupting the stack.
>
>
>Gambit
>
>

 

Re:Calling Function in DLL Crashes When Parameter is const char*

"Thomas Scott" < XXXX@XXXXX.COM >wrote in message
Quote
typedef long PFNADDTEXTURE( const char*, int );
That is not the proper way to declare a typedef for a function pointer. It
needs to be this instead:
typedef long (*PFNADDTEXTURE)( const char*, int );
Quote
static PFNADDTEXTURE* _AddTexture = NULL;
Change that to this instead:
PFNADDTEXTURE _AddTexture = NULL;
Quote
_AddTexture = (PFNADDTEXTURE*)GetProcAddress( dllHandle,
fnRES_AddTexture );
And that to this:
_AddTexture = (PFNADDTEXTURE) GetProcAddress( dllHandle,
fnRES_AddTexture );
Now, with that said, neither the DLL code nor your application code are
specifying any calling convention for the function. The DLL's exported
function is name-mangled and decorated. The formatting of the mangling and
decorating suggests to me that VC++ used __stdcall rather than __cdecl.
BCB, on the ohter hand, uses __cdecl. So, as I mentioned earlier, your
problem is most likely a result of stack corruption due to the application
using a different calling convention than the DLL uses.
Gambit
 

Re:Calling Function in DLL Crashes When Parameter is const char*

I've checked my project settings and have ensured that my dll is using the
__cdecl calling convention. I cannot see that it is a problem. Remember
that all my other functions in this dll work fine. Only any that I create
with const char* parameters fail. Any idea why this particular parameter
type would cause the problem?
Tom
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Thomas Scott" < XXXX@XXXXX.COM >wrote in message
news:422491d5$ XXXX@XXXXX.COM ...

>typedef long PFNADDTEXTURE( const char*, int );

That is not the proper way to declare a typedef for a function pointer.
It
needs to be this instead:

typedef long (*PFNADDTEXTURE)( const char*, int );

>static PFNADDTEXTURE* _AddTexture = NULL;

Change that to this instead:

PFNADDTEXTURE _AddTexture = NULL;

>_AddTexture = (PFNADDTEXTURE*)GetProcAddress( dllHandle,
fnRES_AddTexture );

And that to this:

_AddTexture = (PFNADDTEXTURE) GetProcAddress( dllHandle,
fnRES_AddTexture );

Now, with that said, neither the DLL code nor your application code are
specifying any calling convention for the function. The DLL's exported
function is name-mangled and decorated. The formatting of the mangling
and
decorating suggests to me that VC++ used __stdcall rather than __cdecl.
BCB, on the ohter hand, uses __cdecl. So, as I mentioned earlier, your
problem is most likely a result of stack corruption due to the application
using a different calling convention than the DLL uses.


Gambit


 

Re:Calling Function in DLL Crashes When Parameter is const char*

I've managed to get everything to work. I took my dll code and put it into
my old VC++ 6.0 project and compiled it there. Now it works fine.
Unfortunately, I still don't know why my new version in Visual Studio .NET
doesn't work. I assume there are some differences in the compile options,
but I haven't been able to find anything yet.
Thanks for everyone's help.
Tom
"Thomas Scott" < XXXX@XXXXX.COM >wrote in message
Quote
I've checked my project settings and have ensured that my dll is using the
__cdecl calling convention. I cannot see that it is a problem. Remember
that all my other functions in this dll work fine. Only any that I create
with const char* parameters fail. Any idea why this particular parameter
type would cause the problem?

Tom

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
news:4224ba09$ XXXX@XXXXX.COM ...
>
>"Thomas Scott" < XXXX@XXXXX.COM >wrote in message
>news:422491d5$ XXXX@XXXXX.COM ...
>
>>typedef long PFNADDTEXTURE( const char*, int );
>
>That is not the proper way to declare a typedef for a function pointer.
>It
>needs to be this instead:
>
>typedef long (*PFNADDTEXTURE)( const char*, int );
>
>>static PFNADDTEXTURE* _AddTexture = NULL;
>
>Change that to this instead:
>
>PFNADDTEXTURE _AddTexture = NULL;
>
>>_AddTexture = (PFNADDTEXTURE*)GetProcAddress( dllHandle,
>fnRES_AddTexture );
>
>And that to this:
>
>_AddTexture = (PFNADDTEXTURE) GetProcAddress( dllHandle,
>fnRES_AddTexture );
>
>Now, with that said, neither the DLL code nor your application code are
>specifying any calling convention for the function. The DLL's exported
>function is name-mangled and decorated. The formatting of the mangling
>and
>decorating suggests to me that VC++ used __stdcall rather than __cdecl.
>BCB, on the ohter hand, uses __cdecl. So, as I mentioned earlier, your
>problem is most likely a result of stack corruption due to the
>application
>using a different calling convention than the DLL uses.
>
>
>Gambit
>
>