Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting GetPrivateProfileString() to work on Win2k+

Status
Not open for further replies.

johnlopez2000

Programmer
Aug 2, 2002
90
US
I am creating a program in VB6 that will use a "job ticket" (file created by client side to indicate job specs).

I wish to create this in the format of an INI file.

I was attempting to use GetPrivateProfileString() which is supported in kernel.dll, however is not directly supported in kernel32.dll

I am attempting to use the kernel32.dll replacement GetPrivateProfileStringA().

kernel32.dll shows not function entry point for GetPrivateProfileString(). When I use GetPrivateProfileStringA(), I get a memory error and the VB6 editor crashes.

At this point I am down to writing my own version of the above. Does anyone have any recommendations on the use of the above?

Thanks very much.

John Lopez
Enterprise PDM Architect
john.l.lopez@goodrich.com
 
Please post your API definition of GetPrivateProfileString

As I have no trouble using it on W2k!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
declaration at the module level:

Private Declare Function GetPrivateProfileStringA Lib "kernel32" (ByVal Section$, ByVal Entry$, ByVal NotFound$, ByVal ReturnedString$, ByVal Size%, ByVal Filename$) As Integer


function call in the method:

i = GetPrivateProfileStringA("JOBTICKET", "FILENAME", "", xJtk.Filename, 256, xJtk.JobTicketFilename)


John Lopez
Enterprise PDM Architect
john.l.lopez@goodrich.com
 
Code:
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Is the definition that I use with success.

I suspect your issue may be with defining nsize as integer rather than long.


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
John,

Go with Matt's definition. The one you are trying to use is the old 16-bit VB version
 
I did as you indicated. I get the same error:

The instruction at "0x7c5794c4" referenced memory at "0x00000000". The memory could not be "written".

Click on OK to terminate the program
Click on CANCEL to debug the program

At this point is crashes VB6.

thanks

John Lopez
Enterprise PDM Architect
john.l.lopez@goodrich.com
 
Ok,

Are you still calling the function with the same call...

try
Code:
dim strBuf as String
dim lngLen as Long, lngResult as Long

strBuf = String(255, 0)
lngLen = 255
lngResult = GetPrivateProfileString("JOBTICKET", "FILENAME", "", strBuf, lngLen, xJtk.JobTicketFilename)

    If lngResult > 0 Then
        xJtk.filename = Left$(strBuf, lngResult)
    End If


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
looks like your code works???

my assumption is that this must be low level IO??? where a blocking factor has to be specified??? Is the way I was doing it causing a buffer overflow and thereby a memory error?

thanks for you help.

~(;-}}

John Lopez
Enterprise PDM Architect
john.l.lopez@goodrich.com
 
has the destination variable had any memory allocated?

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
well ... not sure what changed,

but now the stupid thing does not instantiate the data in strBuf, just has 256 nulls???



John Lopez
Enterprise PDM Architect
john.l.lopez@goodrich.com
 
Have you tried putting a value in the default setting?
Code:
lngResult = GetPrivateProfileString("JOBTICKET", "FILENAME", "MyTestDefault", strBuf, lngLen, xJtk.JobTicketFilename)

and see what is returned. I would check teh value of xJtk.Filename and make sure that the file is what you expect it to be. Also what is the value of lngResult? You might consider putting an else condition in the if statement that will pop a messagebox, and print the value of GetLastError() (often not much help as VB sometimes resets the LastError) or err.dllError...

Can't offer much more help right now, but a bit more info along th lines of above may help

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Hi Matt, thanks for your help

I did add the default value, so my code looks like:

Dim strBuf, x, y, z, msg As String
strBuf = String(255, 0)
lngLen = 255

lngResult = GetPrivateProfileString("JOBTICKET", "FILENAME", "INVALID", strBuf, lngLen, xJtk.JobTicketFilename)
y = Left$(strBuf, lngResult)

the above vars states after "y" is executed:

the line read from the file should be: FILENAME=C:\temp5\D0000001.pdf
lngResult: 21
xJtk.JobTicketFilename: c:\temp5\test.jtk
lngLen: 255
strBuf: (255 null chars)
y: (21 null chars)

The content of test.jtk (note- I've tried the text content with and without double quotes):
[JOBTICKET]
FILENAME=C:\temp5\D0000001.pdf
FILEROTATION=PLOT
STAMP=CUSTOM
STAMPCOMPANYNAME=
STAMPCOMPANYCOUNTRY=
STAMPCUSTOMTEXT=some text content
STAMPFONT=ARIAL
STAMPFONTSIZE=10
STAMPWIDTH=80
STAMPLOCATION=TITLEBLOCK
STAMPROTATION=PARALLEL


John Lopez
Enterprise PDM Architect
john.l.lopez@goodrich.com
 
Code:
Dim strBuf, x, y, z, msg As String

does not declare strBuf, x, y, z as strings

I would try this
Code:
Dim strBuf As String, x As String, y As String, z As String, msg As String

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Yep, Just tested your code

It is exactly as you say.

However, declaring the variables as strings resolves it.

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Matt,

thanks so much.

that seems to have fixed it.

John Lopez
Enterprise PDM Architect
john.l.lopez@goodrich.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top