简体   繁体   中英

C++ Calling one function from another

I have a C++ Custom Action Project. I have two functions, RegProductName and GetProductName .

I call RegProductName and it has three possible outcomes. I have these in an if statement that if it is outcome 1 or outcome 2 then i call my second function GetProductName but i can't seem to get it working. Can anyone give me an example of calling one function from another please?

extern "C" UINT __stdcall RegProductName(MSIHANDLE hInstall)
{
AssertSz(FALSE, "debug here");
DebugBreak();

HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
char szProductName[MAX_PATH];


hr = WcaInitialize(hInstall, "RegProductName");
ExitOnFailure(hr, "Failed to initialize");

WcaLog(LOGMSG_STANDARD, "Initialized.");

strcpy(szProductName, Orc_Get_Product_Name());

if(szProductName == "ORCHESTRATOR")
{
    GetProductName();
} 
else if (szProductName == "CORAL")
{
    GetProductName();
}
else 
{
    MsiSetProperty(hInstall, "PRODUCTNAME",  szProductName);
}


LExit:
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}

The error is "Too few arguments in function call when i hover over GetProductName();

extern "C" UINT __stdcall GetProductName(MSIHANDLE hInstall)
{

HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
DWORD Ret;
CHAR *Section = "General";
CHAR szBuffer[MAX_PATH];
CHAR szProductIniFile[MAX_PATH];
char lpszString[MAX_PATH];
int lplValue;


hr = WcaInitialize(hInstall, "GetProductName");
ExitOnFailure(hr, "Failed to initialize");

WcaLog(LOGMSG_STANDARD, "Initialized.");

TCHAR* szValueBuf = NULL;
DWORD cchValueBuf = 0;
UINT uiStat =  MsiGetProperty(hInstall, TEXT("TEMPFOLDER"), TEXT(""), &cchValueBuf);

if (ERROR_MORE_DATA == uiStat)
{
    ++cchValueBuf; 
    szValueBuf = new TCHAR[cchValueBuf];
    if (szValueBuf)
    {
        uiStat = MsiGetProperty(hInstall, TEXT("TEMPFOLDER"), szValueBuf, &cchValueBuf);
    }
}
if (ERROR_SUCCESS != uiStat)
{
    if (szValueBuf != NULL) 
        delete[] szValueBuf;
    return ERROR_INSTALL_FAILURE;
}

strcpy(szProductIniFile,szValueBuf);

Ret = strlen(szProductIniFile);
if(szProductIniFile[Ret-1] != '\\')
   strcat(szProductIniFile,"\\");

strcat(szProductIniFile, "Product.ini");

Ret = GetPrivateProfileString(Section,          // Section Title [General]
                              "PRODUCT_NAME",   // Entry
                              "Orchestrator",   // Default Value
                              szBuffer,         // Address of buffer to read to
                              MAX_PATH,         // Length of buffer
                              szProductIniFile); // .ini file name


if (strlen(szBuffer) == 0)
    strcpy(szBuffer, "ORCHESTRATOR");

if (strlen(szBuffer) >= 3 && (stricmp(szBuffer+strlen(szBuffer)-3,"DEM") == 0))
    lplValue = 1;
else
    lplValue = 0;


MsiSetProperty(hInstall, "PRODUCTNAME",  szBuffer);

LExit:
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}

This does not compare the string content:

if(szProductName == "ORCHESTRATOR")

either use strcmp() or use std::string and == :

if(szProductName == std::string("ORCHESTRATOR"))

Your GetProductName() function takes an argument MSIHANDLE hInstall . You'll need to provide that when calling it. For instance, if you just want to call it with the same handle as RegProductName() was called with:

GetProductName(hInstall);

Your GetProductName looks like this:

extern "C" UINT __stdcall GetProductName(MSIHANDLE hInstall)
                                         \________________/
                                            The Argument

So it needs to take 1 argument, while you are calling it without argumanes at all:

getProductName( );
               ^
               |
            nothing is being passed here

hence the error you're getting. Based on your code you should probably pass your hInstall there:

getProductName( hInstall );

The GetProductName requires one argument of type MSIHANDLE whereas you are calling it without any parameter. Try instead

GetProductName(hInstall);

GetProductName is defined as GetProductName(MSIHANDLE hInstall) , that means you MUST pass relevant MSIHANDLE as parameter. And that's exactly the error you're getting. But you're doing szProductName == "ORCHESTRATOR" - this is not how you compare strings in C. You seem to lack basic knowledge about C. You should not be writing in C or C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM