By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

GetTempPath API

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi,

Instead of using an api call you can use the environment class to
get the temp path.
Environment.GetEnvironmentVariable("TEMP")

Ken

------------------------

"hjackman" <hf********@comcast.net> wrote in message
news:uQ*************@TK2MSFTNGP11.phx.gbl...
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

Nov 21 '05 #2

P: n/a
"hjackman" <hf********@comcast.net> schrieb:
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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a

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

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

Instead of using an api call you can use the environment class to
get the temp path.
Environment.GetEnvironmentVariable("TEMP")

Ken

------------------------

"hjackman" <hf********@comcast.net> wrote in message
news:uQ*************@TK2MSFTNGP11.phx.gbl...
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


Nov 21 '05 #4

P: n/a
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
Nov 21 '05 #5

P: n/a
Why would anyone want to change their environmentsl varibles? Pretty
pointless really

Crouchie1998
MCP MCSE
Nov 21 '05 #6

P: n/a
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


"Crouchie1998" <cr**********@spamcop.net> wrote in message
news:uv**************@TK2MSFTNGP14.phx.gbl... Why would anyone want to change their environmentsl varibles? Pretty
pointless really

Crouchie1998
MCP MCSE

Nov 21 '05 #7

P: n/a
In article <uQ*************@TK2MSFTNGP11.phx.gbl>, hjackman wrote:
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 ()

--
Tom Shelton
Nov 21 '05 #8

P: n/a
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
Nov 21 '05 #9

P: n/a
In article <#d**************@TK2MSFTNGP09.phx.gbl>, Crouchie1998 wrote:
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.

--
Tom Shelton
Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.