tarnic
Posts: 26
Joined: Mon May 18, 2009 10:41 am

DLL uses MSI Property

Hi, I'm writing a simple C++ DLL which opens a text file and writes 2 directory path of the installation.
I'm not an expert C++ Programmer, but I don't understand where is my error. I have also seen all example in the help like SerialValidation and I think to have done the same, but it doesn't work correctly.

Here is the C++ DLL code:

Code: Select all

#include "stdafx.h"

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <msi.h>
#include <MsiDefs.h>
#include <MsiQuery.h>
#include <tchar.h>
#include <stdlib.h>

using namespace std;

#define DLL extern "C" __declspec(dllexport)

DLL int writeProgramConfig(MSIHANDLE hInstall){
	TCHAR pathProgramConfig[1000]; 
	DWORD dwLen = sizeof(pathProgramConfig)/sizeof(pathProgramConfig[0]); 
	// Get the "Program.Config" path property
	if(MsiGetProperty(hInstall, _T("WPC_PROGCONFIG"), pathProgramConfig, &dwLen) != ERROR_SUCCESS) return 1; //fail the installation
    
	// Get the "K_Data" path property
	TCHAR kdata[1000]; 
	dwLen = sizeof(kdata)/sizeof(kdata[0]); 
	if(MsiGetProperty(hInstall, _T("PATH_KDATA"), kdata, &dwLen) != ERROR_SUCCESS) return 1; //fail the installation

	// Get the "K_Data_s" path property
	TCHAR kdatas[1000]; 
	dwLen = sizeof(kdatas)/sizeof(kdatas[0]); 
	if(MsiGetProperty(hInstall, _T("PATH_KDATA_S"), kdatas, &dwLen) != ERROR_SUCCESS) wcscpy_s(kdatas, kdata); //K_DATA_S is K_DATA

//View if the strings are passed
	ofstream myfile;
	myfile.open ("C:\\Documents and Settings\\MyUser\\Desktop\\view.txt", ios::out);
	myfile << "Path " << pathProgramConfig <<"\n";
	myfile << "kdata " << kdata <<"\n";
	myfile << "kdatas " << kdatas <<"\n";
	myfile.close();

//writes the correct file
	myfile.open (pathProgramConfig, ios::out);
	myfile << "[WorkPathLocal]\n";
	myfile << kdata;
	myfile << "[WorkPathServer]\n";
	myfile << kdatas;
	myfile.close();

return 0;
}
WPC_PROGCONFIG is a Setup Property, set as the TXT file (with its path) to write in.
PATH_KDATA is a Setup Property, it's a path to write into the file
PATH_KDATA_S is a Setup Property, it's a path to write into the file. If there isn't, then use the above with wcscpy_s function
Instead of create new Properties, can I use AI folders properties like [#Program.config]???
Or can I pass value into ActionData in Custom Actions? Have you an example?

I created an Attached Custom Action under Show Standard Action, Before Finalization, Install Files.
Sync execution, check return code.
Immediate execution.

When I launch the setup, it executes the DLL function but writes this:
Path 00C8F4F8
kdata 00C8E558
kdatas 00C8ED28

Instead I would like this one:
Path C:\ProgramFiles\Installdir\Program.config
kdata C:\KDATA_path
kdatas C:\KDATA_path

Any help?

Thank you very much in advance!
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: DLL uses MSI Property

Hello,

Because TCHAR evaluates to wchar_t if your project setting has Unicode enabled, you will need to use wofstream instead of ofstream. ofstream by itself can not handle wide characters.
Instead of create new Properties, can I use AI folders properties like [#Program.config]???
I am afraid using syntax like "[#Program.config]" within your custom action is not supported.
Or can I pass value into ActionData in Custom Actions? Have you an example?
You can use [#Program.config] directly in the Custom Action Data field. This would evaluate to the full path Program.config is installed to.
You could also try using a Property Set with Formatted custom action to assign the [#Program.config] to a property, which you could use directly in your script/dll.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
tarnic
Posts: 26
Joined: Mon May 18, 2009 10:41 am

Re: DLL uses MSI Property

Ok, thank you very much, I resolved the problem! Very stupid error...

Return to “Common Problems”