Board index » cppbuilder » SetupFindFirstLine (Searching INF Files)

SetupFindFirstLine (Searching INF Files)


2003-11-15 01:10:14 AM
cppbuilder104
Hello,
has anyone successfully used SetupFindFirstLine with INF files?
(used to retrieve a line in a INF file?)
I've successfully used SetupOpenInfFile which returns a handle to the opened
INF file.
Then attempted to use "SetupFindFirstLine" and "SetupGetStringField"
However, SetupFindFirstLine unexpectedly returns a FALSE value (i.e. the
function could not find the line).
Code is below. Any pointers on what the problem is would be great!
Cheers,
W
char aInfName[] = "C:/Filename.inf";
char *aSection = "section1";
DWORD dwResult;
HINF hInf; //variable to hold the INF handle
UINT ErrorLine; //variable to hold errors returned
HINF hInf;
UINT ErrorLine;
PINFCONTEXT aContext;
DWORD aFieldIndex;
PTSTR aReturnBuffer;
DWORD aReturnBufferSize;
PDWORD aRequiredSize;
BOOL aStrEntryFound;
BOOL aLineFound;
hInf = SetupOpenInfFile (
aInfName,
NULL,
INF_STYLE_WIN4,
&ErrorLine); //line number of the syntax error
if (hInf == INVALID_HANDLE_VALUE)
{
dwResult = GetLastError();
}
else
{
printf ("Searching for entry ..\n");
aLineFound = SetupFindFirstLine(
hInf, // handle to INF file
aSection, // section in which to find a
line
NULL, // optional, key to search for
aContext // context of the found line
);
.....................
 
 

Re:SetupFindFirstLine (Searching INF Files)

"Wes" < XXXX@XXXXX.COM >wrote in message
Quote
However, SetupFindFirstLine unexpectedly returns a FALSE value (i.e.
the function could not find the line).
You are passing an unintialized Context pointer to SetupFindFirstLine().
Just like with most other Win32 API structures, you should be using API
structures on the stack and then using the '&' address operator when passing
them to API functions. In other words:
// *** here - not a pointer ***
INFCONTEXT aContext = {0};
// *** here - passed by '&' ***
aLineFound = SetupFindFirstLine(..., &aContext);
Gambit
 

Re:SetupFindFirstLine (Searching INF Files)

Cheers -- that works fine.
I've attempted to use SetupGetStringField (after finding the corresponding
line returned by SetupFindFirstLine) although this doesn't seem to work.
Can you help?
BOOL aStrEntryFound;
DWORD aFieldIndex =0;
PTSTR aReturnBuffer;
DWORD aReturnBufferSize;
PDWORD aRequiredSize;
aStrEntryFound = SetupGetStringField(
&aContext, // context of the
INF file returned by SetupFindFirstLine
aFieldIndex, // index of the field to
get (set to zero)
aReturnBuffer, // optional, receives
the field
aReturnBufferSize, // size of the
provided buffer
aRequiredSize // optional, buffer
size needed
);
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Wes" < XXXX@XXXXX.COM >wrote in message
news:3fb50c7a$ XXXX@XXXXX.COM ...

>However, SetupFindFirstLine unexpectedly returns a FALSE value (i.e.
>the function could not find the line).

You are passing an unintialized Context pointer to SetupFindFirstLine().
Just like with most other Win32 API structures, you should be using API
structures on the stack and then using the '&' address operator when
passing
them to API functions. In other words:

// *** here - not a pointer ***
INFCONTEXT aContext = {0};


// *** here - passed by '&' ***
aLineFound = SetupFindFirstLine(..., &aContext);


Gambit


 

{smallsort}

Re:SetupFindFirstLine (Searching INF Files)

"Wes" < XXXX@XXXXX.COM >wrote in message
Quote
I've attempted to use SetupGetStringField (after finding the
corresponding line returned by SetupFindFirstLine) although
this doesn't seem to work.
Again, you are passing unintialized pointers to the function, this time for
the ReturnBuffer and RequiredSize parameters. You did not pre-allocate the
buffer before passing it to the function, or assign the pointer to NULL
initially. So you are essentially passing a random memory address as the
ReturnBuffer parameter. As for the RequiredSize parameter, you are doing
the same thing you did last time - passing an unintialized pointer when you
should have been using the '&' operator for a variable that is on the stack
instead.
Notice a pattern emerging here in regards to API functions and passing
parameters? Don't take the API documentation so literally. Last time, the
documentation said a function expected a PINFCONTEXT, so you declared a
variable of type PINFCONTEXT when you should have used INFCONTEXT and the
'&' operator instead. This time, the documentation said a function expected
a PDWORD, so you declared a variable of type PDWORD when you should have
used DWORD and the '&' operator instead. When the documentation says it
needs a data type starting with "P", it usually means a pointer, but it also
usually means that it has to be a pointer to a physical instance in memory,
whether stack or heap.
With that said, try this instead:
BOOL StrEntryFound;
DWORD dwFieldIndex = 0;
LPSTR pReturnBuffer = NULL;
DWORD dwRequiredSize = 0;
// obtain the required size of the string field first
SetupGetStringField(&aContext, dwFieldIndex, NULL, 0, &dwRequiredSize);
if( dwRequiredSize )
{
// allocate the buffer to the required size
pReturnBuffer = (LPSTR) GlobalAlloc(GPTR, dwRequiredSize);
if( pReturnBuffer )
{
// get the string value now
StrEntryFound = SetupGetStringField(&aContext, dwFieldIndex,
pReturnBuffer, dwRequiredSize, &dwRequiredSize);
// use StrEntryFound and pReturnBuffer as needed
// free the allocated memory
GlobalFree(pReturnBuffer);
}
}
Gambit