LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to open panel in previous position?

Part II

Retrieve Position Code

   int LastPos;
    char PanelLeft[10];
    char PanelTop[10];
    int PanelLastTop, PanelLastLeft;
    char buff[50];
    int  Status;
    char *ErrMsg;


 Status = RegReadString (REGKEY_HKLM, "SOFTWARE\\AUXPANEL_TopPos", NULL, PanelTop, 10, (void *) &LastPos);
 if (Status != 0) 
  {
  ErrMsg = GetGeneralErrorString(Status);
  sprintf(buff,"Error setting the %s panel's Top position: %s", "Aux Panel", ErrMsg); 
  }
       // Save top
 LastPos = atoi(PanelTop);
     SetPanelAttribute (auxpanelHandle, ATTR_TOP, LastPos);
   
 Status = RegReadString (REGKEY_HKLM, "SOFTWARE\\AUXPANEL_LeftPos", NULL, PanelLeft, 10, (void *) &LastPos);
 if (Status != 0) 
   c {
  ErrMsg = GetGeneralErrorString(Status);
  sprintf(buff,"Error setting the %s panel's Left position: %s", "Aux Panel", ErrMsg); 
  }
 LastPos = atoi(PanelLeft);// Convert from string to integer
 SetPanelAttribute (auxpanelHandle, ATTR_LEFT, LastPos);

 


Now, the question I have now is how to setup the registry keys so that it doesn't cause an error the first time it is installed and launched?

Message 11 of 12
(959 Views)
Here's a rough answer.  I left your code in red and put my new code in blue.  This still needs better error handling. 

Status = RegReadString (REGKEY_HKLM, "SOFTWARE\\AUXPANEL_TopPos", NULL, PanelTop, 10, (void *) &LastPos);
switch( Status )
{
  case 0:  // Everything is good, nothing to see here...
    break;

  case -5070:  // Reg Key does not exist.  Use default window position
    LastPos = DEFAULT_WINDOW_TOP_POSITION;
    break;

  default:  // An error occured!
    ErrMsg = GetGeneralErrorString(Status);
    sprintf(buff,"Error setting the %s panel's Top position: %s", "Aux Panel", ErrMsg); 
     break;
}

// This is bad.  Check the string length.  Make sure LastPos is assigned some value.
// Set top
LastPos = atoi(PanelTop);
SetPanelAttribute (auxpanelHandle, ATTR_TOP, LastPos);
  
The switch statement will let you handle specific errors.  As you use it twice, you should make it a separate function to ease maintenance. The error code returned if the registry key doesn't exist is -5070.  This is defined as ToolErr_MissingKey in toolbox.h.  You may want to also handle ToolErr_CantAccessKeyValue, ToolErr_MissingKeyValue and ToolErr_WrongKeyValueType. 

These two lines bother me:
LastPos = atoi(PanelTop);
SetPanelAttribute (auxpanelHandle, ATTR_TOP, LastPos);

In both versions, PanelTop may not be initialized.  In the debugger, this is ok as CVI initializes all memory to 0 for you.  In the runtime, these are random bits of memory.  the atoi function may find a value there.  As it might not be null terminated, it may find a very long sequence of numbers.  At least add this line to your error handling:
PanelTop[0] = 0;

----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
0 Kudos
Message 12 of 12
(949 Views)