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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing parameters to/from a DLL

Status
Not open for further replies.

jbsys

Programmer
May 6, 2005
51
CA
Hi;
I am very new to MSC++ and need to create a dll using C++ Vs6.0 so that it will NOT be dependant on the .NET framework. My issue is I don't know how to set up my functions to pass parameters into and out of my dll functions. Can someone please help with an example?

thanks
jbsys.
 
Hi jbsy,
Could you please explain what your looking for a little bit more clearly? You say your dll is made with Vs C++ 6 but where are you wanting to use the functions from? Is it another Vs C++ aplication or is it VB or is it straight C?
Thanks
M
 
I will be calling the functions from a Cognos Powerhouse application. So what I really need is an example of a function inside a dll that would accepted and pass back parameter values to/from the calling program.

IE:
Inside the dll there is a function called "getEnvVar()"
Assume that varName contains the environment variable name "PATH".

string getEnvVar(string varName)
{
char varNameVal[];
varNameVal = getenv(varName);
return varNameVal;
}

My assumption is that getEnvVar() would be taking in the string variable "varName", use the getenv() function to retrieve the value of the environment variable "PATH" and place the result into a string variable called varNameVal, then pass back the varNameVal variable.

Any help would be appreciated.

Thanks
jbsys
 
jbsys,
Ok thanks for making that clearer. You need to declaire your function in a way that the function name is not decoreated and the function is exported from the dll. Something like this:
Code:
extern "C" __declspec(dllexport) char * getEnvVar(char *varName)

Then from the Cognos Powerhouse application you need to import and use the dll function. Remember that you can use the visual studio Depends tool to see what functions are being exported from the dll.

I hope that helps.
M
 
Actually; Here's what I have so far:

#include "stdafx.h"
#include "jalib.h"

#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

/*Get the value of an environment variable.*/
DllExport void javarget(strEnvVar, strEnvVal)
char strEnvVar[40];
char strEnvVal[256];
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
char strTemp[41];
int intStrLen;

/*Truncate the environment variable name passed in by Quick.*/
intStrLen = intStrTrmLen(strEnvVar,40);
strncpy(strTemp,strEnvVar,intStrLen);
strTemp[intStrLen] = '\0';

/*Blank out the string prior to filling it.*/
memset(strEnvVal,' ',256);

/*Return the value of the environment variable, making sure*/
/*that the terminator added by C is removed. */
strcpy(strEnvVal,getenv(strTemp));
strEnvVal[intStrTrmLen(strEnvVal,256)] = ' ';

return TRUE;
}


strEnvVal needs to be returned.

jbsys
 
jbsys,
Ok one way you can "return" the value is to expose a variable from your dll that will hold this value. Something like:
Code:
extern "C" __declspec( dllimport ) char strEnvVal[256];

M
 
jbsys,

Do you need to use reference parameters as opposed to your first idea of just passing a simple string in and returning the same? Don't know Powerhouse but every language I know could deal with that. I.e.
Code:
#define DllExport  extern "C" __declspec( dllexport )

DllExport char* javarget( const char * strEnvVar )
{
    return getenv( strEnvVar );
}
should work fine.

Volker/
 
Volker; When I tested your solution, the getenv (strEnvVar) function returns a null pointer. If I change it to:
return getenv( "HOMEPATH" );

it returns "\Documents and Settings\myhomedir" as I expected. Is there something I need to do to strEnvVar or the result fo the function to actually get the string value of the environment variable?

Thanks
jbsys
 
Did you passed the exact same string, i.e. your function call was something like
Code:
xx = javarget( "HOMEPATH" )
or did you call the function somewhat different?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top