GetTempPath API

H

hjackman

Cany anybody please tell me what I am doing wrong?

The second time I call the GetTempPathW function the temporary path string
is returned; however, if you look at the string in the Autos window you can
see that the string is not Null terminated because there is no quotation
mark at the end of the string. This causes other API calls to screw up. I
can circumvent this by using a temporary variable in the second call and
then copy over the characters to a new variable but I would like to know why
the string isn't terminated.

I don't know if this helps but it did not matter the size of the buffer I
passed to the function. It still did not terminate the string.

Operating System: Windows 2000 Professional

<DllImport("kernel32")> _
Private Shared Function GetTempPathW(ByVal BufferLength As Integer,
<MarshalAs(UnmanagedType.LPWStr)> ByVal Buffer As String) As Integer
End Function

Public Function GetTempPath() As String
Dim RV As Integer
Dim Path As String
Dim Length As Integer

'Call the function the first time to determine the size of the buffer
needed.
RV = GetTempPathW(Length, Nothing)
If (RV = 0) Then Return ""

'Allocate the buffer and call again.
Path = Space(RV)
RV = GetTempPathW(Length, Path)
If (RV = 0) Then Return ""
Return Path
End Function

Thank You
 
K

Ken Tucker [MVP]

Hi,

Instead of using an api call you can use the environment class to
get the temp path.


Environment.GetEnvironmentVariable("TEMP")



Ken
 
H

Herfried K. Wagner [MVP]

hjackman said:
The second time I call the GetTempPathW function the temporary path string
is returned; however, if you look at the string in the Autos window you
can
see that the string is not Null terminated because there is no quotation
mark at the end of the string.

Mhm... wouldn't that mean that the string actually /is/ null-terminated?
<DllImport("kernel32")> _
Private Shared Function GetTempPathW(ByVal BufferLength As Integer,
<MarshalAs(UnmanagedType.LPWStr)> ByVal Buffer As String) As Integer
End Function

Why do you explicitly call the Unicode version of the function?

\\\
Private Declare Auto Function GetTempPath Lib "kernel32.dll" ( _
ByVal nBufferLength As Int32, _
ByVal lpBuffer As String _
) As Int32
///

I am curious why you do not use 'System.IO.Path.GetTempPath'.
 
M

m.posseth

i would prefer
System.IO.Path.GetTempPath



as i know some people in my organization ( do not ask me why) who changed
the names of there environment variables in that sitiation your solution
would faill



regards



Michel Posseth
 
D

Dragon

Hello,

If API function is supposed to change string you passed in, than you
should use StringBuilder for this parameter.
Public Function GetTempPath() As String
Dim RV As Integer
Dim Path As String
Dim Length As Integer

'Call the function the first time to determine the size of the buffer
needed.
RV = GetTempPathW(Length, Nothing)
If (RV = 0) Then Return ""

'Allocate the buffer and call again.
Path = Space(RV)
RV = GetTempPathW(Length, Path)

I don't see whether you assigned something to Length.
If (RV = 0) Then Return ""
Return Path
End Function

Also I wonder why you explicitly use Unicode version of a function. This
way your app will crash on Win9x!

Here's the modified version of your code:

~
Private Declare Unicode Function GetTempPathW Lib "kernel32.dll" ( _
ByVal nBufferLength As Integer, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal lpBuffer As
StringBuilder _
) As Integer

Public Function GetTempPath() As String
Dim RV As Integer
Dim Path As StringBuilder

'Call the function the first time to determine the size of the
buffer needed.
RV = GetTempPathW(0, Nothing)
If RV = 0 Then Return ""

'Allocate the buffer and call again.
Path = New StringBuilder(RV)
RV = GetTempPathW(RV, Path)
If RV = 0 Then Return ""
Return Path.ToString
End Function
~

I hope this helps,
Roman
 
C

Crouchie1998

Why would anyone want to change their environmentsl varibles? Pretty
pointless really

Crouchie1998
MCP MCSE
 
M

m.posseth

did i need to say more :)
as i know some people in my organization ( do not ask me why) who changed
the names of there environment variables in that sitiation your solution
would faill

No serieus i have a collegue working at my IT department who once complaint
to me that his computer was acting strange , ( also with one of the programs
i wrote )
nice thingy was that the error said " out of memory " so i turned his
computer upside down and discovered that he not only renamed his environment
variables ( wich was not causing the problem in this case ) but also had
moved the temppath to a seperate partition of only 1 gb !!!! wich also
contained his swap file !!!!!! after questioning why the %$%$ he did
this he told me that this would prevent fragmentation ( wahaaaa :) and
would speed up his computer well the tears were running spontaniously out
of my eyes


it is prove that a seperate partition for your swap will speed up your
computer because there is no fragmentation on the swap file in this case
however the temp will actually cause framentation on this partition so
instead of speeding it up he was slowing his system down , as he was also
running out of diskspace an my program was creating HTML files to browse on
with the webbrowser control in the temp directory ( Note : it also removes
them after the browse action ) windows was giving a out of memory
exception to my program while it tried to create the file in the temp .

Well in my life as a programmer and a previous occupation at the helpdesk
i know for a fact that the so called "advanced" users are the best to
f***up there systems :)


by the way i have also seen a lot of systems were there was only TMP or TEMP
set at the environment variables so one of the two would then faill when
called
while System.IO.Path.GetTempPath wil work if there is a path set,
regardless of the name it was given . so in my opinion it is the prefered
method .

regards

Michel Posseth
 
T

Tom Shelton

Cany anybody please tell me what I am doing wrong?

The second time I call the GetTempPathW function the temporary path string
is returned; however, if you look at the string in the Autos window you can
see that the string is not Null terminated because there is no quotation
mark at the end of the string. This causes other API calls to screw up. I
can circumvent this by using a temporary variable in the second call and
then copy over the characters to a new variable but I would like to know why
the string isn't terminated.

I don't know if this helps but it did not matter the size of the buffer I
passed to the function. It still did not terminate the string.

Operating System: Windows 2000 Professional

<DllImport("kernel32")> _
Private Shared Function GetTempPathW(ByVal BufferLength As Integer,
<MarshalAs(UnmanagedType.LPWStr)> ByVal Buffer As String) As Integer
End Function

Public Function GetTempPath() As String
Dim RV As Integer
Dim Path As String
Dim Length As Integer

'Call the function the first time to determine the size of the buffer
needed.
RV = GetTempPathW(Length, Nothing)
If (RV = 0) Then Return ""

'Allocate the buffer and call again.
Path = Space(RV)
RV = GetTempPathW(Length, Path)
If (RV = 0) Then Return ""
Return Path
End Function

Thank You

You don't really need to call the API function to get the
temp path. You can just use the System.IO.Path class to get it.

Dim tempPath As String = System.IO.Path.GetTempPath ()
 
C

Crouchie1998

Someone said the same as you Tom a few days ago. Please read the other posts
before adding the same code

Crouchie1998
BA (HONS) MCP MCSE
 
T

Tom Shelton

Someone said the same as you Tom a few days ago. Please read the other posts
before adding the same code

Crouchie1998
BA (HONS) MCP MCSE

I would - except it didn't show in my news reader. Sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top