Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DLL internal functions.

Status
Not open for further replies.

jbsys

Programmer
May 6, 2005
51
0
0
CA
Hi;
I am having trouble with a DLL function that is available to my calling program. The function receives char * parameter and calls another function that lives inside of the the dll. For some reason, the internal function does not get called by the exported function inside the dll, or more correctly, the calling program stalls. Can anyone assist with this? Here is the code.

#include "stdafx.h"
#include "mfcdll.h"
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "ctype.h"
#include "windows.h"
#include "winerror.h"

int intStrTrmLen(char *strSource, int intLength);
int strCaseLower(char *strDestination[ ],char *strSource [ ]);
int intStrConvert(char *strString,char *strNewString,int nLength);
char * strStrConvert(char *strString,char *strNewString,int nLength);
void rtrim( char * string, char * trim );

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CMfcdllApp, CWinApp)
//{{AFX_MSG_MAP(CMfcdllApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CMfcdllApp::CMfcdllApp()


CMfcdllApp theApp;

#define DllImport extern "C" __declspec( dllimport )
#define DllExport extern "C" __declspec( dllexport )

int intStrTrmLen(char *strSource, int intLength)
/*char *strSource;
int intLength;*/
{
/*AFX_MANAGE_STATE(AfxGetStaticModuleState());*/
int intCharIndex;
for(intCharIndex=intLength - 1;
intCharIndex > 0 && !isgraph(strSource[intCharIndex]);
intCharIndex--);
return(intCharIndex + 1);
/* return TRUE;*/
}

int strCaseLower(char strDestination[ ],char strSource [ ])
/*char *strDestination;
char *strSource;*/
{
int intLen, intIndex;

intLen = strlen(reinterpret_cast<const char *>(strSource)) + 1;
for (intIndex=0; intIndex < intLen; intIndex++)
{
strDestination[intIndex] = tolower(strSource[intIndex]);
}

return(intLen - 1);
/*return TRUE;*/
}

/* Convert a Quick string to a C string */
int intStrConvert(char *strString,char *strNewString,int nLength)
/*char *strString;
char *strNewString;
int nLength;*/
{
int intStrLen;

intStrLen = intStrTrmLen(strString,nLength);
strncpy(strNewString,strString,intStrLen);
strNewString[intStrLen] = '\0';
return (strlen(strNewString));
/*return TRUE;*/
}

/* Convert a Quick string to a C string */
char * strStrConvert(char *strString,char *strNewString,int nLength)
/*char *strString;
char *strNewString;
int nLength;*/
{
int intStrLen;

intStrLen = intStrTrmLen(strString,nLength);
strncpy(strNewString,strString,intStrLen);
strNewString[intStrLen] = '\0';
return (strNewString);
}

void rtrim( char * string, char * trim )
{
int i;
for( i = strlen (string) - 1; i >= 0
&& strchr ( trim, string ) != NULL; i-- )
// replace the string terminator:
string = '\0';
}

DllExport TestGetEnvVar(char * parm1)
{
char * parm2="";
int iparm1=strlen(parm1);
char * lparm1 = (char *)iparm1;

//Program stalls when it tries to call the strStrConvert internal function.
strStrConvert(parm1,parm2,iparm1);

rtrim(parm1,(char *)"\0");

char * enval2 = getenv(parm1);
strcpy(parm1,enval2);

return;
}

Here's the .def file I have.

; mfcdll.def : Declares the module parameters for the DLL.

LIBRARY "mfcdll"
DESCRIPTION 'mfcdll Windows Dynamic Link Library'


EXPORTS
ExportedFunction
TestGetEnvVar
intStrTrmLen
intStrTrmLen
strCaseLower
intStrConvert
strStrConvert
rtrim

/*NOTE:
These functions:
intStrTrmLen
intStrTrmLen
strCaseLower
intStrConvert
strStrConvert
rtrim
do NOT need to be exposed to the calling program.
*/





Any help would be appreciated.

thanks
jbsys
 
1. Use CODE tags for your snippets (see Process TGML link on the form).
2. Where is study of symptoms? I'm worried about my dll is not a true call for help.
3. Let's take, for example, your strCaseLower() function:
Code:
{
    int intLen, intIndex;

    intLen = strlen(reinterpret_cast<const char *>(strSource)) + 1;
    for (intIndex=0; intIndex < intLen; intIndex++)
    {
    strDestination[intIndex] = tolower(strSource[intIndex]);
    }
What for reinterpret_cast? Why strlen of strSource? Why +1? Is strSource initialized (for strlen)?
It seems it's the most unsafe routine I have ever seen (last two years;)...
4. Use C++ standard C library header names (<cstring> instead of <string.h>). Put them in stdafx.h for fast compilation.

Present (localize) any problem case, please. Your char array manipulation code is a very dangerous source of troubles, so...
 
ArkM - Thanks for you help on this. However, one should be aware that I am very new to C++ and may not be aware of the best practices approach to creating this type of, or any, dll file. At any rate, I have isolated the problem. When I transfer this code to a regular C program, all functions work fine. In the intStrConvert and strStrConvert functions, there is a statement, "strncpy(strNewString,strString,intStrLen);" it is this statement that is causeing the calling program to crash: EG:

1) calling program calls DLL function "TestGetEnvVar" passing its parameter by reference.

2) "TestGetEnvVar" calls internal "strStrConvert" function passing pointers to the various parameters.

3) "strStrConvert" calls the strncpy function passing pointers to req'd paramemter.

During execution, processing stops when "strStrConvert" encounters the strncpy function. Any ideas why this might occur?

Thanks
jbsys
 
There is a very suspicious code in your snippet:
Code:
DllExport TestGetEnvVar(char * parm1)
{
    char * parm2="";
    int iparm1=strlen(parm1);
    char * lparm1 = (char *)iparm1;

    //Program stalls when it tries to call the   strStrConvert internal function.
    strStrConvert(parm1,parm2,iparm1);
...
The parm2 pointers sets to (constant!) empty C-string literal "" (1 zero byte in statics of your dll body). Then you pass it to strStrConvert as 2nd arg, then try to overwrite in the function. Well, you catch memory exception or memory crash...

The 3rd declaration of (unused) lparm1 is a very strange thing too. Why this pointer to char initialized by int (!)value with casting to pointer to char?

This code can't work properly in dll, console or GUI environment...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top