Running into issue creating shortcut on desktop - installshield

I've just created a custom dialog with a checkbox asking if the user wants to create a desktop shortcut. I used to always include a shortcut I'm not using the AskText() function as I plan on adding more pieces to this page later and want to simplify these few options to this one page.
I get an item on my desktop when I run, but it's not what I expect. The target seems to be pointing to a location on the desktop itself and not the actual executable. Also, this shortcut does not delete on uninstall (I'm assuming this needs to be handled separately anyway) and the shortcut needs admin rights to be manually deleted (which I don't want, for obvious reasons).
Below is my InstallScript code. It is in a custom action that was inserted after InstallFiles.
function MyFunction(hMSI)
STRING szProgramFolder, szItemName, szCommandLine, szWorkingDir;
STRING szShortCutKey, szProgram, szParam, szIconPath;
NUMBER nIcon, nResult;
begin
szProgramFolder = FOLDER_DESKTOP;
szItemName = "myProgram";
szProgram = INSTALLDIR + "myProgram.exe" ;
LongPathToQuote (szProgram, TRUE);
szCommandLine = szProgram;
szWorkingDir = INSTALLDIR;
szIconPath = "";
nIcon = 0;
szShortCutKey = "";
nResult = AddFolderIcon (szProgramFolder, szItemName, szCommandLine,szWorkingDir,
szIconPath, nIcon, szShortCutKey, REPLACE);
end;
I'm not quite sure where I'm going wrong here, although my knowledge of InstallShield (let alone InstallScript) is very limited.

As it turned out, this is a deferred custom action, hence the INSTALLDIR variable is not initialized (nor any other Windows Installer built-in variables). Change it to an Immediate-type custom action (and relocate it to an appropriate location in the execution sequence) and it should work.

To fix the shortcut's parameters, start by ensuring they are correct. Debug your function to verify you are actually passing what you want to. As commented, INSTALLDIR may not be available directly to an InstallScript custom action. A simple way to "debug" would be to add calls like MessageBox(szCommandLine, 0); to key points in your code. If you find you are passing something like C:\Program Files\Company\ProductmyProgram.exe, consider using the ^ operator to concatenate your paths: szProgram = INSTALLDIR ^ "myProgram.exe";.
To uninstall the shortcut, you have to understand that custom actions in MSI projects are not automatically reversed. So use a different approach. Either explicitly code up its removal during uninstall in another action, switch to pure InstallScript where logging will reverse your actions, or go with a proper MSI-based approach. For the last of those, define the shortcut in its own component, and give the component a condition that correlates to a property you set in your UI (or via AskText for now), or skip the condition and just use feature selection by putting the component in a child feature. Then Windows Installer will track and remove the shortcut for you.

Related

Visual Studio MFC change text in Edit Control while typing/dynamically

I am trying to set up a MFC C++ App in Visual Studio 2019 such that modifies the user's text as they are typing.
Current layout is 2 radio buttons,
ID= rdbOn (set to Group = True, with Value int variable m_isOn = 1)
ID= rdbOff, m_isOn value would be = 0
and 1 Edit Control,
ID= txtInputBox, with Value CString variable m_inputString
Currently, for testing I can see how it would work for a button on click, it would take something like the following and just SetDlgItemText of the result. But that would be after they have typed, not WHILE they are typing.
void Onsomebtnclick()
{
//convert CString to String of m_inputString
//do some string manipulation
//convert back to CString
//SetDlgItemText(txtInputBox, result)
}
Update:
got EN_CHANGE to work
I was able to get EN_CHANGE working with the flag suggestion from user #GoGoWorx. However, now I just have a slight problem that the cursor is back to the beginning of the edit control txtInput.
I'm reading about using a CEdit::SetSel but don't know how to use that directly in my code. I tried
CEdit control MFC, placing cursor to end of string after SetWindowText
someDlg::someFunction()
{
//some logic stuff to get a result string
SetDlgItemText(txtInputBox, result);
//need it to set the cursor to the end
//I tried these, but it didn't recognize (expression must have class type?)
//txtInputBox.SetSel(0, -1);
//txtInputBox.SetSel(-1);
}
It sounds like you need to use the ON_EN_CHANGE message-map notification (called after the control has been updated due to typing or pasting for example)
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
ON_EN_CHANGE(IDC_EDIT_CONTROL, &CMyDialog::OnEnChangeEditControl)
END_MESSAGE_MAP()
void CMyDialog::OnEnChangeEditControl()
{
// Copy or call your Onsomebtnclick() here
}
I'm not sure what you're using for the numeric identifier for the edit control, since these are typically upper case defines - replace IDC_EDIT_CONTROL above with your define (possibly txtInputBox, but again, these are normally upper case, so I'm not sure).
Also change CMyDialog for the name of your dialog class too.
Note that we're using the ON_EN_CHANGE message-map handler here instead of the ON_EN_UPDATE, since the ON_EN_CHANGE message is sent after the control has been updated, whereas ON_EN_UPDATE is called just before it's updated.
The message-map handlers are described in the Remarks section of the CEdit control documentation: https://learn.microsoft.com/en-us/cpp/mfc/reference/cedit-class?view=msvc-160
Regarding your concern about modifying things as the user types - this should be fine, since every change (keystroke or paste from clipboard, etc.) should trigger this handler to be called, where you can change whatever you need. Just be sure that when you're updating the control, you don't trigger the ON_EN_CHANGE again and end up in a recursive 'change' loop.
You might be able to do this with some sort of flag to indicate you're the one updating the control, as opposed to the user, however it's probably better to subclass the CEdit control to do what you're wanting. There are a few examples out there of how to do this (it's not as difficult as it might sound), for example:
https://www.codeproject.com/Articles/27376/Avoiding-EN-CHANGE-notifications

How can i change property in InstallScript

I want to change property value to selected text in a dialog.
this is my sample source.
#include "ifx.h"
STRING outPath;
export prototype MyFunction(HWND);
function OnFirstUIBefore()
NUMBER nResult, nSetupType, nvSize, nUser;
STRING szTitle, szMsg, szQuestion, svName, svCompany, szFile, szDir;
STRING szLicenseFile;
BOOL bCustom, bIgnore1, bIgnore2;
begin
Dlg_SdAskDestPath:
nResult = SdAskDestPath(szTitle, szMsg, INSTALLDIR, 0);
if (nResult = BACK) goto Dlg_SdAskDestPath;
Dlg_AskOutPath:
nResult = AskDestPath(szTitle, szmsg, szDir, 0);
if (nResult = BACK) goto Dlg_SdAskDestPath;
outPath = szDir;
MyFunction(ISMSI_HANDLE);
return 0;
end;
function MyFunction(hMSI)
STRING value;
begin
MsiSetProperty(hMSI, "OutPutPath", outPath);
end;
OutPutPath used in custom action after finish install.
But OutPutPath was not changed when read in custom action.
I think I must not use ISMSI_HANDLE. But i don't know what i have to use instead.
I tried to make custom action which load Install scripts's method MyFunction after finish install.
It worked well, But the global variable outPath was nul..
Please teach me how can i do this if you know.
Thank you.
At a minimum, you must use a public property, that is one with a name that does not contain any lowercase letters. You may also have to list it in SecureCustomProperties in order to allow users to modify it, if you support installation in restricted environments.
However I'm not certain the exact scenario described by your comment:
I tried to make custom action which load Install scripts's method MyFunction after finish install.
If this scenario is truly after the end of the Windows Installer portion of the installation (an InstallScript MSI runs code both before and after), properties as a whole may not survive to do what you need. To support reading the value at that time you will have to consider other approaches, such as writing the value in the registry, or into a file (e.g. in SUPPORTDIR).

Does the Windows shell support multiple shell property handlers?

I was just trying out the Windows app sample for the Recipe Property Handler which is available here and I modified it to be used on .doc files instead of .recipe files:
const WCHAR c_szRecipeFileExtension[] = L".doc";
But, this seemed to overwrite the previous Office handler's properties with itself, which begs the question, does the Windows shell support multiple shell property handlers, or can you only use one at a time for a given file type? If its possible, what am I missing from the code or logic in the sample?
I couldn't find a concrete answer on MSDN for this question.
No.
But there is a variant you can use (I dont like it but I dont see any additional variant). Save previous Property handler CLSID when you register your own. And when shell request the property that you cannot process - just create instance of previous handler and pass request to them.
CoCreateInstance(SavedCLSID, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IPropertyStore, PS)
PS.QueryInterface(IInitializeWithStream, IWS)
IWS.Initialize(Stream, Mode)
PS.GetValue(AUnknownKey)

InstallShield Run As local account if not ran by admin

I have a InstallShield InstallScript project that needs to be ran with administrative rights. In a nutshell from the InstallShield I need to:
Detect if the installer currently has administrative privileges.
If setup.exe is not being ran with admin rights spawn a new instance of the setup.exe using a local admin account/password then close the old (non-privledged) setup.exe.
So far I know I can do something like this to find if I have admin rights:
//---------------------------------------------------------------------------
// Run As Utilities Library
//---------------------------------------------------------------------------
// Include Ifx.h for built-in InstallScript function prototypes.
#include "Ifx.h"
//---------------------------------------------------------------------------
export prototype UserRightsCheck();
function UserRightsCheck()
begin
MessageBox(INSTALLPROPERTY_INSTALLLOCATION, INFORMATION);
if(USER_ADMINISTRATOR) then
MessageBox("hello Admin", INFORMATION); // testing only
// do nothing we are an admin
else
MessageBox("hello user", SEVERE); // testing only
RunAsAdmin();
endif;
end;
export prototype RunAsAdmin();
function RunAsAdmin()
begin
STRING username = "myUserID";
STRING password = "myPassword";
STRING filepath = INSTALLPROPERTY_INSTALLLOCATION;
RunAsUserAccount(username,password,filepath);
end;
export prototype RunAsUserAccount();
function RunAsUserAccount()
STRING username;
STRING password;
STRING filepath;
begin
/*
Is this the best way to do this? this is the function I need help with
This seems like a hack
*/
if ( SYSINFO.WINNT.bWinXP ) then
LAAW_SHELLEXECUTEVERB = "open"; // target PC is on Windows XP
else
LAAW_SHELLEXECUTEVERB = "runas"; // Windows 7 (or Vista)
endif;
LaunchApplication(
filepath
,"" // Arguments
,"" // Directory
,SW_NORMAL // Use Window Mode
,0
,LAAW_OPTION_WAIT_INCL_CHILD | LAAW_OPTION_USE_SHELLEXECUTE
);
end;
How do I relaunch the installer though? This can be done in Wise Package Studio and many other tools but I haven't found the answer how to do it in this one yet.
I know I could probably do a runas.exe or psexec.exe but that feels like a hack and sounds like a poor practice. After about a day of reading I am still not sure how to do this though.
Could someone please point me in the right direction of the proper way to do this in InstallShield?
Tell the user in the MessageBox to relaunch the setup with admin rights. Explain that they can do this by right clicking the setup.exe in Explorer and then clicking "run as admin" and then OK in the UAC prompt. This is the conceptually "clean" way to do it, since you are doing nothing unexpected.
As I have already stated in the comment above, do think twice about Installscript MSI. It is extremely buggy. The better option is a Basic MSI with Installscript custom action code.
A basic MSI is a standard MSI, and the Installscript custom actions do not interfere with the setup GUI or the overall MSI operation. In Installscript MSI the whole installation sequence is Installscript controlled, and this causes serious bugs - some of which have no workarounds or fixes (even years after their discovery).
Perhaps this is along the lines of what you are looking for: How can I make the installer Run as admin. Also check this.
If you use an MSI instead of Installscript and use the built-in MSI concept of elevated rights, you can use MSI properties set at the command line to install anywhere with elevated rights: msiexec /i MySetup.MSI USER=OneUser /PASS=PassWord /qn. Just stating what is possible and perhaps easier.

string too long with MsiGetProperty with Installshield Installscript

I am using MsiGetProperty to get string parameter value from the installer.
And after that I am calling a managed dll and I pass the that value:
nvBufferSize = MAX_STRING;
MsiGetProperty (hMSI, "DBHMS", sDbHost, nvBufferSize);
when I pass the value of sDbHost is like this when I receive it from managed code:
srvdata-02NULNULNULNULNULNUL......
however in the interface I wrote just "srvdata-02".
With that same code it was fine with Installshield 2010, now I am upgrading it to installshield 2012.
Do you have any solution with that please?
There were some behavior changes to MsiGetProperty awhile back. Try setting nvBufferSize to MAX_SIZE instead of MAX_STRING. Also check the return code of MsiGetProperty to see if it equals ERROR_MORE_DATA or if it's returning some other code. Finally check the value of nvBufferSize to see how many bytes are needed.
BTW, if you are just trying to marshal a property over to managed code, you might want to conisder looking into Deployment Tools Framework (DTF) in Windows Installer XML (WiX). This is a very nice SDK that allows you to write managed code custom actions and package them up as if they are native Win32 libraries. InstallShield can then easily use this as an MSI DLL custom action.
DTF provides an interop library and a session object that can be used like:
Deployment Tools Foundation (DTF) Managed Custom Actions
Reasons DTF is Better
As ridiculous as it may seem, here's a working InstallScript solution for you:
nvBufferSize = MAX_STRING;
nResult = MsiGetProperty( ISMSI_HANDLE, szPropertyName, svValue, nvBufferSize );
if( nResult = ERROR_MORE_DATA ) then
MsiGetProperty( ISMSI_HANDLE, szPropertyName, svValue, nvBufferSize );
endif;
The first attempt returns the actual buffer size needed. If it is more then max string (1024?), the second call gets the whole thing.
Alternatively, I found I could assign nvBufferSize to larger value right off the bat e.g. 4096 and use that with a single call (assuming the data was no longer that limit). The double call, however, is more fool proof.
According to: https://msdn.microsoft.com/en-us/library/aa370134(v=vs.85).aspx the api function is actually designed to return the buffersize by passing an empty literal ("") instead of a string variable. InstallScript 2013 throws a compilation error at you if you try that though...

Resources