⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faq80.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:


<HTML>

<HEAD>

   <TITLE>Determine the path to the Windows Desktop directory and other special folders</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>







<H3>

Determine the path to the Windows Desktop directory and other special folders

</H3>

<P>

Use the API <TT>SHGetSpecialFolder</TT> function. <TT>SHGetSpecialFolder</TT> is prototyped in <TT>SHLOBJ.H.</TT>

This function allows you to retrieve a <TT>pidl</TT> for various directories in the shell, such as the Windows desktop

, the startup directory, and the My Documents folder. Once you have a <TT>pidl</TT>, you can convert the <TT>pidl</TT>

to a path string through the <TT>SHGetPathFromIDList</TT> API function.

</P>

<P>

<TT>SHGetSpecialFolder</TT> takes three arguments. The first argument is an <TT>HWND</TT> that specifies an owner

window for any dialog boxes or message boxes that might appear during the call (I'm not sure why a dialog box would

appear). The second argument is an integer id that determines which folder you are trying to find. The possible values

are

</P>

<PRE>

<UL>

<LI>CSIDL_BITBUCKET              Recycle bin

<LI>CSIDL_CONTROLS               Control Panel

<LI>CSIDL_DESKTOP                Windows desktop

<LI>CSIDL_DESKTOPDIRECTORY       Directory for the desktop

<LI>CSIDL_DRIVES                 My Computer

<LI>CSIDL_FONTS                  Fonts directory

<LI>CSIDL_NETHOOD                Network Neighborhood

<LI>CSIDL_NETWORK                Network Neighborhood virtual folder

<LI>CSIDL_PERSONAL               My Documents

<LI>CSIDL_PRINTERS               Printers

<LI>CSIDL_PROGRAMS               Program groups

<LI>CSIDL_RECENT                 Most recent documents list

<LI>CSIDL_SENDTO                 Send To menu items

<LI>CSIDL_STARTMENU              Taskbar Start menu items

<LI>CSIDL_STARTUP                Startup directory

<LI>CSIDL_TEMPLATES              Document templates </UL></PRE>

<P>

The last argument is the address of a <TT>pidl</TT>. <TT>SHGetSpecialFolderLocation</TT>

writes the result <TT>pidl</TT> to this address.

</P>

<P>

The code example below demonstrates how to use <TT>SHGetSpecialFolderLocation</TT>.

</P>

<pre>

<font color="navy">//----------------------------------------------------------------------</font>

<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>

<b>{</b>

    LPITEMIDLIST  pidl<b>;</b>

    LPMALLOC      pShellMalloc<b>;</b>

    <b>char</b>          szDir<b>[</b>MAX_PATH<b>]</b><b>;</b>



    <font color="navy">// SHGetSpecialFolderLocation generates a PIDL. The memory for the PIDL</font>

    <font color="navy">// is allocated by the shell, and should be freed using the shell</font>

    <font color="navy">// mallocator COM object. Use SHGetMalloc to retrieve the malloc object</font>

    <b>if</b><b>(</b>SUCCEEDED<b>(</b>SHGetMalloc<b>(</b><b>&</b>pShellMalloc<b>)</b><b>)</b><b>)</b>

    <b>{</b>

        <font color="navy">// if we were able to get the shell malloc object, then</font>

        <font color="navy">// proceed by fetching the pidl for the windows desktop</font>

        <b>if</b><b>(</b>SUCCEEDED<b>(</b>SHGetSpecialFolderLocation<b>(</b>NULL<b>,</b>

                                                CSIDL_DESKTOPDIRECTORY<b>,</b>

                                                <b>&</b>pidl<b>)</b><b>)</b><b>)</b>

        <b>{</b>

            <font color="navy">// return is true if success</font>

            <b>if</b><b>(</b>SHGetPathFromIDList<b>(</b>pidl<b>,</b> szDir<b>)</b><b>)</b>

            <b>{</b>

                <font color="navy">// set one caption to the directory path</font>

                Label1<b>-></b>Caption <b>=</b> szDir<b>;</b>

            <b>}</b>



            pShellMalloc<b>-></b>Free<b>(</b>pidl<b>)</b><b>;</b>

        <b>}</b>



        pShellMalloc<b>-></b>Release<b>(</b><b>)</b><b>;</b>

    <b>}</b>

<b>}</b>

<font color="navy">//----------------------------------------------------------------------</font>

</pre>

<P>

When I run the example code on my system, the path to the windows desktop is

<TT>C:\Windows\Desktop</TT>. I expanded the example to retrieve directories

for some of the other special folders. The figure below shows the results.

</P>

<BR>

<IMG SRC="images/specialfolder.gif" BORDER=0 ALIGN="BOTTOM">  <BR>

<H4>Figure 1. Results of the <TT>SHGetSpecialFolder</TT> function</H4>



<P>

<B>Note:</B> Notice that some of the path names are blank. Some of special folders in the shell

do not have a corresponding directory in the file system.

</P>

<P>

<B>Note:</B> The FAQ on <A TARGET=_top HREF="faq62.htm">browsing for a folder</A> contains an explanation of

<TT>SHGetMalloc</TT>.

</P>



<BR>

<TABLE  BORDER=1 CELLPADDING=10 CELLSPACING=0 WIDTH="100%">

<TR> <TD colspan = 2><B>Downloads for this FAQ</B> </TD> </TR>

<TR> <TD><TT><A HREF="download/specialfolder.zip" >specialfolder.zip </A></TT></TD><TD>BCB3 project that demonstrates <TT>SHGetSpecialFolder</TT>.</TD> </TR>

<TR> <TD><TT><A HREF="download/specialfolderx.zip" >specialfolderx.zip </A></TT></TD><TD>Same project, includes an EXE.</TD> </TR>

</TABLE>                         



</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -