cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Rincewind
Level 3

Cannot execute VBScript function inside InstallScript

Hi,
I've used the following function through several versions of Installshield, but it doesn't seem to be working in InstallShield 2010.

prototype BOOL ValidateString(STRING, VARIANT);

function BOOL ValidateString(szString,szPattern)
OBJECT oRegEx;
BOOL MatchFound;
begin

//try to create the RegEx object
try
set oRegEx = CoCreateObject("VBScript.RegExp");
catch
MessageBox("CoCreateObject Failed- "+ Err.Decription, SEVERE);
endcatch;
oRegEx.Pattern = szPattern;
oRegEx.IgnoreCase = 0;
MatchFound = oRegEx.Test(szString);
set oRegEx = NOTHING;
return MatchFound;
end;


It is called with the following code snippets to validate a proper email address format:
[CODE]
// real pattern: ^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})$
emailPattern='^(([^<>()[\\]\\\\.,;:\\s@\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\"]+)*)|(\\".+\\"))@(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,})$';
if (!ValidateString(svMailAddr,emailPattern)) then
[/CODE]

It always takes the "then" clause in IS2010. It has worked, unchanged, in all previous versions of IS back to IS7.
Anyone have any idea what changed in IS2010 to make this no longer work?
Labels (1)
0 Kudos
(5) Replies
Rincewind
Level 3

Turns out both parameters need to be declared "VARIANT" in the prototype definition. Wonder what changed between releases.

prototype BOOL ValidateString(VARIANT, VARIANT);
0 Kudos
joshstechnij
Level 10 Flexeran
Level 10 Flexeran

I'm not sure what the issue could have been here. With your original code (assuming I am understanding the intention of this code correctly), I can get true or false results from ValidateString with the original function prototype. I am passing two string variables (for szString and szPattern) to ValidateString, one with the pattern posted, and the other containing some text or an email address.

There haven't been any changes to the core script engine for a while (with the exception of a change made for calling DLL functions) that could explain the behavior you are seeing.
0 Kudos
anom217
Level 8

I still can't get this to work. I tried having szString and szPattern both set as STRING, then as VARIANT, and still doesn't work. I added a line to display the boolean value in the function

if(MatchFound = TRUE) then
MessageBox("match found", INFORMATION);
else
MessageBox("match not found", INFORMATION);
endif;

I'm still only getting the else clause in my if-else condition. I switched the if check from MatchFound = TRUE to MatchFound = FALSE, and it still always takes the else. This makes me think something is wrong and MatchFound is not getting a true or false result.
0 Kudos
anom217
Level 8

okay, it seems my MatchFound boolean has an error value. I'm not sure why my function isn't working. Does VBScript have to be setup to work correctly? No catch message is thrown when creating oRegEx object in the try block.
0 Kudos
Rincewind
Level 3

I don't do anything special to enable VBScript, but it does need to exist on the system you are running the installer on. All the code is as you see it. There is one thing I didn't mention in the earlier posts: In the calling routine, I have the emailPattern declared as a WSTRING. In IS2009 and before, I just defined it as a STRING. I'm pretty sure I made that change before changing the protoype declaration, so it may be that the combination is what was needed.

One thing I know for sure, do not try to pass the pattern as a literal. That will cause all sorts of issues.

Here are the currently working code snippets for IS2010:

The calling routine (actually a custom dialog):
[CODE]
WSTRING emailPattern;
// real pattern: ^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})$
emailPattern='^(([^<>()[\\]\\\\.,;:\\s@\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\"]+)*)|(\\".+\\"))@(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,})$';
if (!ValidateString(svMailAddr,emailPattern)) then
MessageBox ("Invalid format for an email address.", WARNING);
dlgCtrl = GetDlgItem (hwndDlg, RES_MAIL_ADDR);
SetFocus (dlgCtrl);
CtrlSelectText (szDialogName, RES_MAIL_ADDR);
endif;
[/CODE]
The function itself:

// function prototype declaration
prototype BOOL ValidateString(VARIANT, VARIANT);

///////////////////////////////////////////////////////////////////////////////////////////////
//
// ValidateString
//
// This function uses the VBScript regular expression object
// found in Windows Scripting Host v5 and above to validate
// strings in IS Script.
//
// Refer to- http://msdn.microsoft.com/scripting for more information
// about the RegExp object and VBScript
//
////////////////////////////////////////////////////////////////////////////////////////////////
function BOOL ValidateString(szString,szPattern)
OBJECT oRegEx;
BOOL MatchFound;
begin

//try to create the RegEx object
try
set oRegEx = CoCreateObject("VBScript.RegExp");
catch
MessageBox("CoCreateObject Failed- " + Err.Decription, SEVERE);
endcatch;
oRegEx.Pattern = szPattern;
oRegEx.IgnoreCase = 0;
MatchFound = oRegEx.Test(szString);
set oRegEx = NOTHING;
return MatchFound;
end;



If you are getting an error code, you will need to see what that code means to VBScript. Otherwise, I would expect a problem with your regular expression. I would set it up in a separate VBscript for testing, using the regular VBscript error mechanism to see what it is telling you. One you get it working there, then transfer the pattern to your InstallScript. When you make the transfer, you will need to convert every "\" to "\\" or Installscript will eat them.
0 Kudos