How to create exe which is dependent on a dll.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Micorsoft (raj_at_siripuram.net)
Date: 03/12/04


Date: Fri, 12 Mar 2004 15:51:31 -0500

Hi All,

I have created a simple dll( functions). Now I have to wrap this dll in a exe, so that some dos based applications can call my dll functions.
I tried to create a consol application and made it dependent on my dll project.

I call my dll function in the code and try to compile, I got errors.

Here is the compiler out put.

--------------------Configuration: GetServerBat - Win32 Debug--------------------
Compiling...
GetServerBat.cpp
H:\temp\GetServerBat\GetServerBat.cpp(11) : error C2039: 'SfmConfigGetServerBat' : is not a member of '`global namespace''
H:\temp\GetServerBat\GetServerBat.cpp(11) : error C2065: 'SfmConfigGetServerBat' : undeclared identifier
H:\temp\GetServerBat\GetServerBat.cpp(11) : error C2153: hex constants must have at least one hex digit
Error executing cl.exe.

GetServerBat.exe - 3 error(s), 0 warning(s)
-----------------------------------------------------------------------------------------------------------------

mydll function code
------------------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "c_vb.h"

double __stdcall AddOne(double a)
{
 a++;
 return a;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

// Local
const char* SFM_CONFIG_FILE_NAME = "C:\\temp\\sfmConfig.ini";
const int DEFAULT_STR_SIZE = 512;
const int nSize = 64;
char* SFM_CONFIG_PRIMARY_SERVER = "PRIMARY_SERVER";
char* pReturnedStr;

// Export
const char* SFM_CONFIG_UNKNOWN = "Unknown";

///////////////////////////////////////////////////////////////////////////////

int __stdcall SfmConfigGetServer ( const char* pAppName, char* pReturnedStr, char pWhich )
{

 FILE * pFile;

 pFile = fopen ( SFM_CONFIG_FILE_NAME,"r");

 if ( pFile == NULL ) // No file exist, return with error code
   return -999;

 fclose (pFile);
 

 switch ( pWhich )
      {
         case 'P':
            SFM_CONFIG_PRIMARY_SERVER = "PRIMARY_SERVER";
   break;

         case 'B':
            SFM_CONFIG_PRIMARY_SERVER = "BACKUP_SERVER";
            break;

         case 'D':
            SFM_CONFIG_PRIMARY_SERVER = "DEVELOPMENT_SERVER";
            break;

         case 'T':
            SFM_CONFIG_PRIMARY_SERVER = "TEST_SERVER";
            break;

         default:
            SFM_CONFIG_PRIMARY_SERVER = "UNKNOWN_SERVER";
   break;
      }

    if ( !pWhich )
        return -100; //Not a valid flag for server query.

    if ( !pAppName )
        return -101; //Not a valid AppName

    if ( !pReturnedStr /*|| nSize < 1*/ )
        return -102; //Not a valid reference parameter.

    char str[DEFAULT_STR_SIZE];
    str[0] = '\0';

    ::GetPrivateProfileString ( pAppName, SFM_CONFIG_PRIMARY_SERVER, SFM_CONFIG_UNKNOWN,
        str, DEFAULT_STR_SIZE - 1, SFM_CONFIG_FILE_NAME );

    int nLen = strlen ( str );
    if ( nLen > nSize )
        return -103; //The return variable is not a valid.

    strcpy ( pReturnedStr, str );

    return 1; //Success.
}

int __stdcall SfmConfigGetPrimaryServer ( const char* pAppName, char* pReturnedStr )
{
 char primary = 'P';

 return SfmConfigGetServer ( pAppName, pReturnedStr, primary );
}

int __stdcall SfmConfigGetBackupServer ( const char* pAppName, char* pReturnedStr )
{

 char backup = 'B';

 return SfmConfigGetServer ( pAppName, pReturnedStr, backup );

}

int __stdcall SfmConfigGetDevelopmentServer ( const char* pAppName, char* pReturnedStr )
{
 char primary = 'D';

 return SfmConfigGetServer ( pAppName, pReturnedStr, primary );
}

int __stdcall SfmConfigGetTestServer ( const char* pAppName, char* pReturnedStr )
{
 char primary = 'T';

 return SfmConfigGetServer ( pAppName, pReturnedStr, primary );
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Create bat file based on user request.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//int __stdcall SfmConfigGetServerBat(const char* pAppName, char pOutFile )
int __stdcall SfmConfigGetServerBat( const char* pAppName, const char* pOutFile )

{

 //const char* pAppName="AFS";
 int rtn;
 

 char* pPrimary ;
 char* pdevelopement ;
 char* pBackup ;
 char* pTest ;
 
 pPrimary = "PRIMARY_SERVER";
 pdevelopement = "DEVELOPMENT_SERVER";
 pBackup = "BACKUP_SERVER";
 pTest = "TEST_SERVER";

 rtn = SfmConfigGetPrimaryServer ( pAppName, pPrimary );
 if ( rtn < 0 )
  return -998;

 rtn = SfmConfigGetBackupServer ( pAppName, pdevelopement );
 if ( rtn < 0 )
  return -997;

 rtn = SfmConfigGetDevelopmentServer ( pAppName, pBackup );
 if ( rtn < 0 )
  return -996;

 rtn = SfmConfigGetTestServer ( pAppName, pTest );
 if ( rtn < 0 )
  return -996;

  FILE * pFile;

  //pFile = fopen ("c:\\temp\\myfile.bat","w+");

  pFile = fopen ( pOutFile ,"w+");

  fprintf ( pFile, "set PRIMARY_SERVER=%s\n", pPrimary );
  fprintf ( pFile, "set DEVELOPMENT_SERVER=%s\n", pdevelopement );
  fprintf ( pFile, "set BACKUP_SERVER=%s\n", pBackup );
  fprintf ( pFile, "set TEST_SERVER=%s\n", pTest );

  fclose (pFile);

  return 0;

  
}

---------------------------------------------------------------------------------------
Mydef file
---------------------------------------------------------------------------------------
LIBRARY c_function
DESCRIPTION 'A C++ dll that can be called from VB'
EXPORTS
 AddOne @1
 SfmConfigGetPrimaryServer @23
 SfmConfigGetBackupServer @45
 SfmConfigGetServerBat @66

---------------------------------------------------------------------------------------
Myheader file
---------------------------------------------------------------------------------------
#include <windows.h>
#include <winsock.h>
#include <stdio.h>

---------------------------------------------------------------------------------------
MyConsol application CPP file
---------------------------------------------------------------------------------------

#include "stdafx.h"

int main(int argc, char* argv[])
{
 
 printf("Hello World!\n");
 return ::SfmConfigGetServerBat("AFS", "C:\temp\xxx.bat");

}

This is my first C++ project. I don know too much about it.
Thanks a lot for any help.

-RajS



Relevant Pages

  • Re: How to create exe which is dependent on a dll.
    ... int __stdcall SfmConfigGetServer (const char* pAppName, char* pReturnedStr, char pWhich) ...
    (microsoft.public.vc.language)
  • Re: LoadLibrary() in eVC
    ... System APIs in CE take wide char, ... wide-char, just fools the compiler. ... TCHAR - always use WCHAR, L"", L''. ... > I'm trying to load a dll in my eVC application using "LoadLibrary". ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Program crashes before it starts!!!
    ... DLL could have been the source of the problem. ... string (char*) variables we pass to the DLL function being malloc'd ... int main ... char *mystr = NULL; ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Program crashes before it starts!!!
    ... DLL could have been the source of the problem. ... Both are using the Multi-Threaded Debug versions of the CRT ... string (char*) variables we pass to the DLL function being malloc'd ... int main ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Pass char* to C# callback function
    ... "public delegate int Test;" ... How about open project properties and allow unsafe blocks, ... > party dll) to call my delegate works fine. ... > callback and pass back a char array....then my app needs to convert it ...
    (microsoft.public.dotnet.languages.csharp)