mirror of
https://github.com/ApfelTeeSaft/reactos.git
synced 2026-09-03 04:13:31 +00:00
[RAPPS] Final commit
- Removed atlex svn path=/branches/GSoC_2017/rapps/; revision=75702
This commit is contained in:
-2312
File diff suppressed because it is too large
Load Diff
-339
@@ -1,339 +0,0 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
# atlex
|
||||
Provides additional templates and function helpers for Microsoft Active Template Library
|
||||
|
||||
## Building
|
||||
- The _.h_ files can be used individually. However, we do encourage you to include the entire library project and reference it in dependant projects of your solution, as atlex might develop some non-inline code over time.
|
||||
- The _libatl.vcxproj_ requires Microsoft Visual Studio 2010 SP1 and _..\\..\\include_ folder with _common.props_, _Debug.props_, _Release.props_, _Win32.props_, and _x64.props_ files to customize building process for individual applications.
|
||||
|
||||
## Usage
|
||||
1. Clone the repository into your solution folder.
|
||||
2. Add the _libatl.vcxproj_ to your solution.
|
||||
3. Add atlex's _include_ folder to _Additional Include Directories_ in your project's C/C++ settings.
|
||||
4. Add a new reference to atlex project from your project's common properties.
|
||||
5. Include _.h_ files from atlex as needed:
|
||||
```C
|
||||
#include <atlstr.h>
|
||||
#include <atlshlwapi.h>
|
||||
#include <iostream>
|
||||
|
||||
void main()
|
||||
{
|
||||
ATL::CAtlString sPath;
|
||||
PathCanonicalize(sPath, _T("C:\\Windows\\Temp\\test\\.."));
|
||||
std::cout << (LPCTSTR)sPath << std::endl;
|
||||
}
|
||||
```
|
||||
-498
@@ -1,498 +0,0 @@
|
||||
/*
|
||||
Copyright 1991-2017 Amebis
|
||||
|
||||
This file is part of atlex.
|
||||
|
||||
atlex is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
atlex is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with atlex. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "atlex.h"
|
||||
#include <atlcoll.h>
|
||||
#include <atlstr.h>
|
||||
#include <WinCrypt.h>
|
||||
|
||||
///
|
||||
/// \defgroup ATLCryptoAPI Cryptography API
|
||||
/// Integrates ATL classes with Microsoft Cryptography API
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Obtains the subject or issuer name from a certificate [CERT_CONTEXT](https://msdn.microsoft.com/en-us/library/windows/desktop/aa377189.aspx) structure and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [CertGetNameString function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376086.aspx)
|
||||
///
|
||||
inline DWORD CertGetNameStringA(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_ void *pvTypePara, _Out_ ATL::CAtlStringA &sNameString)
|
||||
{
|
||||
// Query the final string length first.
|
||||
DWORD dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
|
||||
|
||||
// Allocate buffer on heap to format the string data into and read it.
|
||||
LPSTR szBuffer = sNameString.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, szBuffer, dwSize);
|
||||
sNameString.ReleaseBuffer(dwSize);
|
||||
return dwSize;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Obtains the subject or issuer name from a certificate [CERT_CONTEXT](https://msdn.microsoft.com/en-us/library/windows/desktop/aa377189.aspx) structure and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [CertGetNameString function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376086.aspx)
|
||||
///
|
||||
inline DWORD CertGetNameStringW(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_ void *pvTypePara, _Out_ ATL::CAtlStringW &sNameString)
|
||||
{
|
||||
// Query the final string length first.
|
||||
DWORD dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
|
||||
|
||||
// Allocate buffer on heap to format the string data into and read it.
|
||||
LPWSTR szBuffer = sNameString.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, szBuffer, dwSize);
|
||||
sNameString.ReleaseBuffer(dwSize);
|
||||
return dwSize;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Retrieves data that governs the operations of a hash object. The actual hash value can be retrieved by using this function.
|
||||
///
|
||||
/// \sa [CryptGetHashParam function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379947.aspx)
|
||||
///
|
||||
inline BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ ATL::CAtlArray<BYTE> &aData, _In_ DWORD dwFlags)
|
||||
{
|
||||
DWORD dwHashSize;
|
||||
|
||||
if (CryptGetHashParam(hHash, dwParam, NULL, &dwHashSize, dwFlags)) {
|
||||
if (aData.SetCount(dwHashSize)) {
|
||||
if (CryptGetHashParam(hHash, dwParam, aData.GetData(), &dwHashSize, dwFlags)) {
|
||||
return TRUE;
|
||||
} else {
|
||||
aData.SetCount(0);
|
||||
return FALSE;
|
||||
}
|
||||
} else {
|
||||
SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Exports a cryptographic key or a key pair from a cryptographic service provider (CSP) in a secure manner.
|
||||
///
|
||||
/// \sa [CryptExportKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379931.aspx)
|
||||
///
|
||||
inline BOOL CryptExportKey(_In_ HCRYPTKEY hKey, _In_ HCRYPTKEY hExpKey, _In_ DWORD dwBlobType, _In_ DWORD dwFlags, _Out_ ATL::CAtlArray<BYTE> &aData)
|
||||
{
|
||||
DWORD dwKeyBLOBSize;
|
||||
|
||||
if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) {
|
||||
if (aData.SetCount(dwKeyBLOBSize)) {
|
||||
if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, aData.GetData(), &dwKeyBLOBSize)) {
|
||||
return TRUE;
|
||||
} else {
|
||||
aData.SetCount(0);
|
||||
return FALSE;
|
||||
}
|
||||
} else {
|
||||
SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/// @}
|
||||
|
||||
|
||||
namespace ATL
|
||||
{
|
||||
namespace Crypt
|
||||
{
|
||||
/// \addtogroup ATLCryptoAPI
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// PCCERT_CONTEXT wrapper class
|
||||
///
|
||||
class CCertContext : public ATL::CObjectWithHandleDuplT<PCCERT_CONTEXT>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Destroys the certificate context.
|
||||
///
|
||||
/// \sa [CertFreeCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376075.aspx)
|
||||
///
|
||||
virtual ~CCertContext()
|
||||
{
|
||||
if (m_h)
|
||||
CertFreeCertificateContext(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Creates the certificate context.
|
||||
///
|
||||
/// \return
|
||||
/// - TRUE when creation succeeds;
|
||||
/// - FALSE when creation fails. For extended error information, call `GetLastError()`.
|
||||
/// \sa [CertCreateCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376033.aspx)
|
||||
///
|
||||
inline BOOL Create(_In_ DWORD dwCertEncodingType, _In_ const BYTE *pbCertEncoded, _In_ DWORD cbCertEncoded)
|
||||
{
|
||||
HANDLE h = CertCreateCertificateContext(dwCertEncodingType, pbCertEncoded, cbCertEncoded);
|
||||
if (h) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Destroys the certificate context.
|
||||
///
|
||||
/// \sa [CertFreeCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376075.aspx)
|
||||
///
|
||||
virtual void InternalFree()
|
||||
{
|
||||
CertFreeCertificateContext(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Duplicates the certificate context.
|
||||
///
|
||||
/// \param[in] h Object handle of existing certificate context
|
||||
/// \return Duplicated certificate context handle
|
||||
/// \sa [CertDuplicateCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376045.aspx)
|
||||
///
|
||||
virtual HANDLE InternalDuplicate(_In_ HANDLE h) const
|
||||
{
|
||||
return CertDuplicateCertificateContext(h);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// PCCERT_CHAIN_CONTEXT wrapper class
|
||||
///
|
||||
class CCertChainContext : public ATL::CObjectWithHandleDuplT<PCCERT_CHAIN_CONTEXT>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Destroys the certificate chain context.
|
||||
///
|
||||
/// \sa [CertFreeCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376073.aspx)
|
||||
///
|
||||
virtual ~CCertChainContext()
|
||||
{
|
||||
if (m_h)
|
||||
CertFreeCertificateChain(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Creates the certificate chain context.
|
||||
///
|
||||
/// \return
|
||||
/// - TRUE when creation succeeds;
|
||||
/// - FALSE when creation fails. For extended error information, call `GetLastError()`.
|
||||
/// \sa [CertGetCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376078.aspx)
|
||||
///
|
||||
inline BOOL Create(_In_opt_ HCERTCHAINENGINE hChainEngine, _In_ PCCERT_CONTEXT pCertContext, _In_opt_ LPFILETIME pTime, _In_opt_ HCERTSTORE hAdditionalStore, _In_ PCERT_CHAIN_PARA pChainPara, _In_ DWORD dwFlags, __reserved LPVOID pvReserved)
|
||||
{
|
||||
HANDLE h;
|
||||
if (CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &h)) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Destroys the certificate chain context.
|
||||
///
|
||||
/// \sa [CertFreeCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376073.aspx)
|
||||
///
|
||||
virtual void InternalFree()
|
||||
{
|
||||
CertFreeCertificateChain(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Duplicates the certificate chain context.
|
||||
///
|
||||
/// \param[in] h Object handle of existing certificate chain context
|
||||
/// \return Duplicated certificate chain context handle
|
||||
/// \sa [CertDuplicateCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376045.aspx)
|
||||
///
|
||||
virtual HANDLE InternalDuplicate(_In_ HANDLE h) const
|
||||
{
|
||||
return CertDuplicateCertificateChain(h);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// HCERTSTORE wrapper class
|
||||
///
|
||||
class CCertStore : public ATL::CObjectWithHandleT<HCERTSTORE>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Closes the certificate store.
|
||||
///
|
||||
/// \sa [CertCloseStore function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376026.aspx)
|
||||
///
|
||||
virtual ~CCertStore()
|
||||
{
|
||||
if (m_h)
|
||||
CertCloseStore(m_h, 0);
|
||||
}
|
||||
|
||||
///
|
||||
/// Opens the certificate store.
|
||||
///
|
||||
/// \return
|
||||
/// - TRUE when creation succeeds;
|
||||
/// - FALSE when creation fails. For extended error information, call `GetLastError()`.
|
||||
/// \sa [CertOpenStore function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376559.aspx)
|
||||
///
|
||||
inline BOOL Create(_In_ LPCSTR lpszStoreProvider, _In_ DWORD dwEncodingType, _In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_ DWORD dwFlags, _In_opt_ const void *pvPara)
|
||||
{
|
||||
HANDLE h = CertOpenStore(lpszStoreProvider, dwEncodingType, hCryptProv, dwFlags, pvPara);
|
||||
if (h) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Closes the certificate store.
|
||||
///
|
||||
/// \sa [CertCloseStore function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376026.aspx)
|
||||
///
|
||||
virtual void InternalFree()
|
||||
{
|
||||
CertCloseStore(m_h, 0);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// HCRYPTPROV wrapper class
|
||||
///
|
||||
class CContext : public ATL::CObjectWithHandleT<HCRYPTPROV>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Releases the cryptographi context.
|
||||
///
|
||||
/// \sa [CryptReleaseContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380268.aspx)
|
||||
///
|
||||
virtual ~CContext()
|
||||
{
|
||||
if (m_h)
|
||||
CryptReleaseContext(m_h, 0);
|
||||
}
|
||||
|
||||
///
|
||||
/// Acquires the cryptographic context.
|
||||
///
|
||||
/// \return
|
||||
/// - TRUE when creation succeeds;
|
||||
/// - FALSE when creation fails. For extended error information, call `GetLastError()`.
|
||||
/// \sa [CryptAcquireContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379886.aspx)
|
||||
///
|
||||
inline BOOL Create(_In_opt_ LPCTSTR szContainer, _In_opt_ LPCTSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
|
||||
{
|
||||
HANDLE h;
|
||||
if (CryptAcquireContext(&h, szContainer, szProvider, dwProvType, dwFlags)) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Releases the cryptographic context.
|
||||
///
|
||||
/// \sa [CryptReleaseContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380268.aspx)
|
||||
///
|
||||
virtual void InternalFree()
|
||||
{
|
||||
CryptReleaseContext(m_h, 0);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// HCRYPTHASH wrapper class
|
||||
///
|
||||
class CHash : public ATL::CObjectWithHandleDuplT<HCRYPTHASH>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Destroys the hash context.
|
||||
///
|
||||
/// \sa [CryptDestroyHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379917.aspx)
|
||||
///
|
||||
virtual ~CHash()
|
||||
{
|
||||
if (m_h)
|
||||
CryptDestroyHash(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Creates the hash context.
|
||||
///
|
||||
/// \return
|
||||
/// - TRUE when creation succeeds;
|
||||
/// - FALSE when creation fails. For extended error information, call `GetLastError()`.
|
||||
/// \sa [CryptCreateHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379908.aspx)
|
||||
///
|
||||
inline BOOL Create(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTKEY hKey, _In_ DWORD dwFlags)
|
||||
{
|
||||
HANDLE h;
|
||||
if (CryptCreateHash(hProv, Algid, hKey, dwFlags, &h)) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Destroys the hash context.
|
||||
///
|
||||
/// \sa [CryptDestroyHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379917.aspx)
|
||||
///
|
||||
virtual void InternalFree()
|
||||
{
|
||||
CryptDestroyHash(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Duplicates the hash context.
|
||||
///
|
||||
/// \param[in] h Object handle of existing hash context
|
||||
/// \return Duplicated hash context handle
|
||||
/// \sa [CryptDuplicateHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379919.aspx)
|
||||
///
|
||||
virtual HANDLE InternalDuplicate(_In_ HANDLE h) const
|
||||
{
|
||||
HANDLE hNew = NULL;
|
||||
return CryptDuplicateHash(h, NULL, 0, &hNew) ? hNew : NULL;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// HCRYPTKEY wrapper class
|
||||
///
|
||||
class CKey : public ATL::CObjectWithHandleDuplT<HCRYPTKEY>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Destroys the key.
|
||||
///
|
||||
/// \sa [CryptDestroyKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379918.aspx)
|
||||
///
|
||||
virtual ~CKey()
|
||||
{
|
||||
if (m_h)
|
||||
CryptDestroyKey(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Generates the key.
|
||||
///
|
||||
/// \sa [CryptGenKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379941.aspx)
|
||||
///
|
||||
inline BOOL Generate(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags)
|
||||
{
|
||||
HANDLE h;
|
||||
if (CryptGenKey(hProv, Algid, dwFlags, &h)) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
///
|
||||
/// Imports the key.
|
||||
///
|
||||
/// \sa [CryptImportKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380207.aspx)
|
||||
///
|
||||
inline BOOL Import(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) CONST BYTE *pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags)
|
||||
{
|
||||
HANDLE h;
|
||||
if (CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h)) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
///
|
||||
/// Imports the public key.
|
||||
///
|
||||
/// \sa [CryptImportPublicKeyInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380209.aspx)
|
||||
///
|
||||
inline BOOL ImportPublic(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo)
|
||||
{
|
||||
HANDLE h;
|
||||
if (CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h)) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Destroys the key.
|
||||
///
|
||||
/// \sa [CryptDestroyKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379918.aspx)
|
||||
///
|
||||
virtual void InternalFree()
|
||||
{
|
||||
CryptDestroyKey(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Duplicates the key.
|
||||
///
|
||||
/// \param[in] h Object handle of existing key
|
||||
/// \return Duplicated key handle
|
||||
/// \sa [CryptDuplicateKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379920.aspx)
|
||||
///
|
||||
virtual HANDLE InternalDuplicate(_In_ HANDLE h) const
|
||||
{
|
||||
HANDLE hNew = NULL;
|
||||
return CryptDuplicateKey(h, NULL, 0, &hNew) ? hNew : NULL;
|
||||
}
|
||||
};
|
||||
|
||||
/// @}
|
||||
}
|
||||
}
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
Copyright 1991-2017 Amebis
|
||||
|
||||
This file is part of atlex.
|
||||
|
||||
atlex is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
atlex is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with atlex. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <eaptypes.h>
|
||||
|
||||
|
||||
namespace ATL
|
||||
{
|
||||
namespace EAP
|
||||
{
|
||||
///
|
||||
/// \defgroup ATLEAPAPI Extensible Authentication Protocol API
|
||||
/// Integrates ATL classes with Microsoft EAP API
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// EAP_ATTRIBUTE wrapper class
|
||||
///
|
||||
class CEAPAttribute : public EAP_ATTRIBUTE
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Initializes a new EAP attribute set to eatReserved.
|
||||
///
|
||||
CEAPAttribute()
|
||||
{
|
||||
eaType = eatReserved;
|
||||
dwLength = 0;
|
||||
pValue = NULL;
|
||||
}
|
||||
|
||||
///
|
||||
/// Destroys the EAP attribute.
|
||||
///
|
||||
~CEAPAttribute()
|
||||
{
|
||||
if (pValue)
|
||||
delete pValue;
|
||||
}
|
||||
};
|
||||
|
||||
/// @}
|
||||
}
|
||||
}
|
||||
-654
@@ -1,654 +0,0 @@
|
||||
/*
|
||||
Copyright 1991-2017 Amebis
|
||||
|
||||
This file is part of atlex.
|
||||
|
||||
atlex is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
atlex is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with atlex. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atlconv.h>
|
||||
#include <atldef.h>
|
||||
#include <atlstr.h>
|
||||
|
||||
#ifndef ATL_STACK_BUFFER_BYTES
|
||||
///
|
||||
/// Size of the stack buffer in bytes used for initial system function call
|
||||
///
|
||||
/// Some system functions with variable length output data fail for
|
||||
/// insufficient buffer sizes, and return an exact buffer length required.
|
||||
/// The function helpers use a fixed size stack buffer first. If the stack
|
||||
/// buffer really prooved sufficient, the helper allocates the exact length
|
||||
/// output on heap and copies the data without calling the system function
|
||||
/// again. Otherwise it allocates the exact length output on heap and retries.
|
||||
///
|
||||
/// \note
|
||||
/// Decrease this value in case of stack overflow.
|
||||
///
|
||||
#define ATL_STACK_BUFFER_BYTES 1024
|
||||
#endif
|
||||
|
||||
|
||||
namespace ATL
|
||||
{
|
||||
///
|
||||
/// \defgroup ATLSysHandles System Handles
|
||||
/// Simplifies work with object handles of various type
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Base abstract template class to support generic object handle keeping
|
||||
///
|
||||
/// It provides basic operators and methods common to all descendands of this class establishing a base to ease the replacement of native object handle type with classes in object-oriented approach.
|
||||
///
|
||||
template <class T> class CObjectWithHandleT
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Datatype of the object handle this template class handles
|
||||
///
|
||||
typedef T HANDLE;
|
||||
|
||||
///
|
||||
/// Initializes a new class instance with the object handle set to NULL.
|
||||
///
|
||||
inline CObjectWithHandleT() : m_h(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
///
|
||||
/// Initializes a new class instance with an available object handle.
|
||||
///
|
||||
/// \param[in] h Initial object handle value
|
||||
///
|
||||
inline CObjectWithHandleT(_In_opt_ HANDLE h) : m_h(h)
|
||||
{
|
||||
}
|
||||
|
||||
/// \name Operators for transparent usage of this class
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Auto-typecasting operator
|
||||
///
|
||||
/// \return Object handle
|
||||
///
|
||||
inline operator HANDLE() const
|
||||
{
|
||||
return m_h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the object handle value when the object handle is a pointer to a value (class, struct, etc.).
|
||||
///
|
||||
/// \return Object handle value
|
||||
///
|
||||
inline HANDLE*& operator*() const
|
||||
{
|
||||
ATLENSURE(m_h != NULL);
|
||||
return *m_h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the object handle reference.
|
||||
/// \return Object handle reference
|
||||
///
|
||||
inline HANDLE* operator&()
|
||||
{
|
||||
ATLASSERT(m_h == NULL);
|
||||
return &m_h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Provides object handle member access when the object handle is a pointer to a class or struct.
|
||||
///
|
||||
/// \return Object handle
|
||||
///
|
||||
inline HANDLE operator->() const
|
||||
{
|
||||
ATLASSERT(m_h != NULL);
|
||||
return m_h;
|
||||
}
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Comparison operators
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Tests if the object handle is NULL.
|
||||
///
|
||||
/// \return
|
||||
/// - Non zero when object handle is NULL;
|
||||
/// - Zero otherwise.
|
||||
///
|
||||
inline bool operator!() const
|
||||
{
|
||||
return m_h == NULL;
|
||||
}
|
||||
|
||||
///
|
||||
/// Is handle less than?
|
||||
///
|
||||
/// \param[in] h Object handle to compare against
|
||||
/// \return
|
||||
/// - Non zero when object handle is less than h;
|
||||
/// - Zero otherwise.
|
||||
///
|
||||
inline bool operator<(_In_opt_ HANDLE h) const
|
||||
{
|
||||
return m_h < h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Is handle less than or equal to?
|
||||
///
|
||||
/// \param[in] h Object handle to compare against
|
||||
/// \return
|
||||
/// - Non zero when object handle is less than or equal to h;
|
||||
/// - Zero otherwise.
|
||||
///
|
||||
inline bool operator<=(_In_opt_ HANDLE h) const
|
||||
{
|
||||
return m_h <= h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Is handle greater than or equal to?
|
||||
///
|
||||
/// \param[in] h Object handle to compare against
|
||||
/// \return
|
||||
/// - Non zero when object handle is greater than or equal to h;
|
||||
/// - Zero otherwise.
|
||||
///
|
||||
inline bool operator>=(_In_opt_ HANDLE h) const
|
||||
{
|
||||
return m_h >= h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Is handle greater than?
|
||||
///
|
||||
/// \param[in] h Object handle to compare against
|
||||
/// \return
|
||||
/// - Non zero when object handle is greater than h;
|
||||
/// - Zero otherwise.
|
||||
///
|
||||
inline bool operator>(_In_opt_ HANDLE h) const
|
||||
{
|
||||
return m_h > h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Is handle not equal to?
|
||||
///
|
||||
/// \param[in] h Object handle to compare against
|
||||
/// \return
|
||||
/// - Non zero when object handle is not equal to h;
|
||||
/// - Zero otherwise.
|
||||
///
|
||||
inline bool operator!=(_In_opt_ HANDLE h) const
|
||||
{
|
||||
return !operator==(h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Is handle equal to?
|
||||
///
|
||||
/// \param[in] h Object handle to compare against
|
||||
/// \return
|
||||
/// - Non zero when object handle is equal to h;
|
||||
/// - Zero otherwise.
|
||||
///
|
||||
inline bool operator==(_In_opt_ HANDLE h) const
|
||||
{
|
||||
return m_h == h;
|
||||
}
|
||||
|
||||
/// @}
|
||||
|
||||
///
|
||||
/// Sets a new object handle for the class
|
||||
///
|
||||
/// When the current object handle of the class is non-NULL, the object is destroyed first.
|
||||
///
|
||||
/// \param[in] h New object handle
|
||||
///
|
||||
inline void Attach(_In_opt_ HANDLE h)
|
||||
{
|
||||
if (m_h)
|
||||
InternalFree();
|
||||
m_h = h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Dismisses the object handle from this class
|
||||
///
|
||||
/// \return Object handle
|
||||
///
|
||||
inline HANDLE Detach()
|
||||
{
|
||||
HANDLE h = m_h;
|
||||
m_h = NULL;
|
||||
return h;
|
||||
}
|
||||
|
||||
///
|
||||
/// Destroys the object
|
||||
///
|
||||
inline void Free()
|
||||
{
|
||||
if (m_h) {
|
||||
InternalFree();
|
||||
m_h = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/// @}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Abstract member function that must be implemented by child classes to do the actual object destruction.
|
||||
///
|
||||
virtual void InternalFree() = 0;
|
||||
|
||||
protected:
|
||||
HANDLE m_h; ///< Object handle
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// Base abstract template class to support object handle keeping for objects that support handle duplication
|
||||
///
|
||||
template <class T>
|
||||
class CObjectWithHandleDuplT : public CObjectWithHandleT<T>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Duplicates and returns a new object handle.
|
||||
///
|
||||
/// \return Duplicated object handle
|
||||
///
|
||||
inline HANDLE GetDuplicate() const
|
||||
{
|
||||
return m_h ? InternalDuplicate(m_h) : NULL;
|
||||
}
|
||||
|
||||
///
|
||||
/// Duplicates an object handle and sets a new object handle.
|
||||
///
|
||||
/// \param[in] h Object handle of existing object
|
||||
/// \return
|
||||
/// - TRUE when duplication succeeds;
|
||||
/// - FALSE when duplication fails. In case of failure obtaining the extended error information is object type specific (for example: `GetLastError()`).
|
||||
///
|
||||
inline BOOL DuplicateAndAttach(_In_opt_ HANDLE h)
|
||||
{
|
||||
if (m_h)
|
||||
InternalFree();
|
||||
|
||||
return h ? (m_h = InternalDuplicate(h)) != NULL : (m_h = NULL, TRUE);
|
||||
}
|
||||
|
||||
//
|
||||
// Do not allow = operators. They are semantically ambigious:
|
||||
// Do they attach the class to the existing instance of object, or do they duplicate it?
|
||||
// To avoid confusion, user should use Attach() and Duplicate() methods explicitly.
|
||||
//
|
||||
//inline const CObjectWithHandleDuplT<T>& operator=(_In_ const HANDLE src)
|
||||
//{
|
||||
// Attach(src ? InternalDuplicate(src) : NULL);
|
||||
// return *this;
|
||||
//}
|
||||
|
||||
//inline const CObjectWithHandleDuplT<T>& operator=(_In_ const CObjectWithHandleDuplT<T> &src)
|
||||
//{
|
||||
// Attach(src.m_h ? InternalDuplicate(src.m_h) : NULL);
|
||||
// return *this;
|
||||
//}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Abstract member function that must be implemented by child classes to do the actual object handle duplication.
|
||||
///
|
||||
/// \param[in] h Object handle of existing object
|
||||
/// \return Duplicated object handle
|
||||
///
|
||||
virtual HANDLE InternalDuplicate(_In_ HANDLE h) const = 0;
|
||||
};
|
||||
|
||||
/// @}
|
||||
|
||||
///
|
||||
/// \defgroup ATLStrFormat String Formatting
|
||||
/// Formatted string generation
|
||||
///
|
||||
/// \par Example
|
||||
/// \code
|
||||
/// // Please note the PCSTR typecasting invokes an operator to return
|
||||
/// // pointer to formatted buffer rather than class reference itself.
|
||||
/// cout << (PCSTR)(CStrFormatA("%i is less than %i.\n", 1, 5));
|
||||
/// \endcode
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Base template class to support string formatting using `printf()` style templates
|
||||
///
|
||||
template<typename BaseType, class StringTraits>
|
||||
class CStrFormatT : public CStringT<BaseType, StringTraits>
|
||||
{
|
||||
public:
|
||||
/// \name Initializing string using template in memory
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Initializes a new string and formats its contents using `printf()` style template.
|
||||
///
|
||||
/// \param[in] pszFormat String template using `printf()` style
|
||||
///
|
||||
CStrFormatT(_In_z_ _Printf_format_string_ PCXSTR pszFormat, ...)
|
||||
{
|
||||
ATLASSERT(AtlIsValidString(pszFormat));
|
||||
|
||||
va_list argList;
|
||||
va_start(argList, pszFormat);
|
||||
FormatV(pszFormat, argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Initializing string using template in resources
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Initializes a new string and formats its contents using `printf()` style template in resources.
|
||||
///
|
||||
/// \param[in] nFormatID Resource ID of the string template using `printf()` style
|
||||
///
|
||||
CStrFormatT(_In_ UINT nFormatID, ...)
|
||||
{
|
||||
CStringT strFormat(GetManager());
|
||||
ATLENSURE(strFormat.LoadString(nFormatID));
|
||||
|
||||
va_list argList;
|
||||
va_start(argList, nFormatID);
|
||||
FormatV(strFormat, argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
///
|
||||
/// Initializes a new string and formats its contents using `printf()` style template in resources.
|
||||
///
|
||||
/// \param[in] hInstance Resource module handle
|
||||
/// \param[in] nFormatID Resource ID of the string template using `printf()` style
|
||||
///
|
||||
CStrFormatT(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
|
||||
{
|
||||
CStringT strFormat(GetManager());
|
||||
ATLENSURE(strFormat.LoadString(hInstance, nFormatID));
|
||||
|
||||
va_list argList;
|
||||
va_start(argList, nFormatID);
|
||||
FormatV(strFormat, argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
///
|
||||
/// Initializes a new string and formats its contents using `printf()` style template in resources.
|
||||
///
|
||||
/// \param[in] hInstance Resource module handle
|
||||
/// \param[in] wLanguageID Resource language
|
||||
/// \param[in] nFormatID Resource ID of the string template using `printf()` style
|
||||
///
|
||||
CStrFormatT(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
|
||||
{
|
||||
CStringT strFormat(GetManager());
|
||||
ATLENSURE(strFormat.LoadString(hInstance, nFormatID, wLanguageID));
|
||||
|
||||
va_list argList;
|
||||
va_start(argList, nFormatID);
|
||||
FormatV(strFormat, argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
/// }@
|
||||
};
|
||||
|
||||
///
|
||||
/// Wide character implementation of a class to support string formatting using `printf()` style templates
|
||||
///
|
||||
typedef CStrFormatT< wchar_t, StrTraitATL< wchar_t, ChTraitsCRT< wchar_t > > > CStrFormatW;
|
||||
|
||||
///
|
||||
/// Single-byte character implementation of a class to support string formatting using `printf()` style templates
|
||||
///
|
||||
typedef CStrFormatT< char, StrTraitATL< char, ChTraitsCRT< char > > > CStrFormatA;
|
||||
|
||||
///
|
||||
/// TCHAR implementation of a class to support string formatting using `printf()` style templates
|
||||
///
|
||||
typedef CStrFormatT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > > > CStrFormat;
|
||||
|
||||
|
||||
///
|
||||
/// Base template class to support string formatting using `FormatMessage()` style templates
|
||||
///
|
||||
template<typename BaseType, class StringTraits>
|
||||
class CStrFormatMsgT : public CStringT<BaseType, StringTraits>
|
||||
{
|
||||
public:
|
||||
/// \name Initializing string using template in memory
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Initializes a new string and formats its contents using `FormatMessage()` style template.
|
||||
///
|
||||
/// \param[in] pszFormat String template using `FormatMessage()` style
|
||||
///
|
||||
CStrFormatMsgT(_In_z_ _FormatMessage_format_string_ PCXSTR pszFormat, ...)
|
||||
{
|
||||
ATLASSERT(AtlIsValidString(pszFormat));
|
||||
|
||||
va_list argList;
|
||||
va_start(argList, pszFormat);
|
||||
FormatMessageV(pszFormat, &argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Initializing string using template in resources
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Initializes a new string and formats its contents using `FormatMessage()` style template in resources.
|
||||
///
|
||||
/// \param[in] nFormatID Resource ID of the string template using `FormatMessage()` style
|
||||
///
|
||||
CStrFormatMsgT(_In_ UINT nFormatID, ...)
|
||||
{
|
||||
CStringT strFormat(GetManager());
|
||||
ATLENSURE(strFormat.LoadString(nFormatID));
|
||||
|
||||
va_list argList;
|
||||
va_start(argList, nFormatID);
|
||||
FormatMessageV(strFormat, &argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
///
|
||||
/// Initializes a new string and formats its contents using `FormatMessage()` style template in resources.
|
||||
///
|
||||
/// \param[in] hInstance Resource module handle
|
||||
/// \param[in] nFormatID Resource ID of the string template using `FormatMessage()` style
|
||||
///
|
||||
CStrFormatMsgT(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
|
||||
{
|
||||
CStringT strFormat(GetManager());
|
||||
ATLENSURE(strFormat.LoadString(hInstance, nFormatID));
|
||||
|
||||
va_list argList;
|
||||
va_start(argList, nFormatID);
|
||||
FormatMessageV(strFormat, &argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
///
|
||||
/// Initializes a new string and formats its contents using `FormatMessage()` style template in resources.
|
||||
///
|
||||
/// \param[in] hInstance Resource module handle
|
||||
/// \param[in] wLanguageID Resource language
|
||||
/// \param[in] nFormatID Resource ID of the string template using `FormatMessage()` style
|
||||
///
|
||||
CStrFormatMsgT(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
|
||||
{
|
||||
CStringT strFormat(GetManager());
|
||||
ATLENSURE(strFormat.LoadString(hInstance, nFormatID, wLanguageID));
|
||||
|
||||
va_list argList;
|
||||
va_start(argList, nFormatID);
|
||||
FormatMessageV(strFormat, &argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
/// @}
|
||||
};
|
||||
|
||||
///
|
||||
/// Wide character implementation of a class to support string formatting using `FormatMessage()` style templates
|
||||
///
|
||||
typedef CStrFormatMsgT< wchar_t, StrTraitATL< wchar_t, ChTraitsCRT< wchar_t > > > CStrFormatMsgW;
|
||||
|
||||
///
|
||||
/// Single-byte character implementation of a class to support string formatting using `FormatMessage()` style templates
|
||||
///
|
||||
typedef CStrFormatMsgT< char, StrTraitATL< char, ChTraitsCRT< char > > > CStrFormatMsgA;
|
||||
|
||||
///
|
||||
/// TCHAR implementation of a class to support string formatting using `FormatMessage()` style templates
|
||||
///
|
||||
typedef CStrFormatMsgT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > > > CStrFormatMsg;
|
||||
|
||||
/// @}
|
||||
|
||||
/// \defgroup ATLMemSanitize Auto-sanitize Memory Management
|
||||
/// Sanitizes memory before dismissed
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// A heap template that sanitizes each memory block before it is destroyed or reallocated
|
||||
///
|
||||
/// This template is typcally used to extend one of the base ATL heap management classes.
|
||||
///
|
||||
/// \par Example
|
||||
/// \code
|
||||
/// CParanoidHeap<CWin32Heap> myHeap;
|
||||
/// \endcode
|
||||
///
|
||||
/// \note
|
||||
/// CParanoidHeap introduces a performance penalty. However, it provides an additional level of security.
|
||||
/// Use for security sensitive data memory storage only.
|
||||
///
|
||||
/// \sa [Memory Management Classes](https://msdn.microsoft.com/en-us/library/44yh1z4f.aspx)
|
||||
///
|
||||
template <class BaseHeap>
|
||||
class CParanoidHeap : public BaseHeap {
|
||||
public:
|
||||
///
|
||||
/// Sanitizes memory before freeing it
|
||||
///
|
||||
/// \param[in] p Pointer to heap memory block
|
||||
///
|
||||
virtual void Free(_In_opt_ void* p)
|
||||
{
|
||||
// Sanitize then free.
|
||||
SecureZeroMemory(p, GetSize(p));
|
||||
BaseHeap::Free(p);
|
||||
}
|
||||
|
||||
///
|
||||
/// Safely reallocates the memory block by sanitizing memory at the previous location
|
||||
///
|
||||
/// This member function always performs the following steps (regardless of the current and new size):
|
||||
/// 1. Allocates a new memory block,
|
||||
/// 2. Copies the data,
|
||||
/// 3. Sanitizes the old memory block,
|
||||
/// 4. Frees the old memory block.
|
||||
///
|
||||
/// \param[in] p Pointer to heap memory block
|
||||
/// \param[in] nBytes New size in bytes
|
||||
/// \return Pointer to the new heap memory block
|
||||
///
|
||||
virtual /*_Ret_opt_bytecap_(nBytes)*/ void* Reallocate(_In_opt_ void* p, _In_ size_t nBytes)
|
||||
{
|
||||
// Create a new sized copy.
|
||||
void *pNew = Allocate(nBytes);
|
||||
size_t nSizePrev = GetSize(p);
|
||||
CopyMemory(pNew, p, nSizePrev);
|
||||
|
||||
// Sanitize the old data then free.
|
||||
SecureZeroMemory(p, nSizePrev);
|
||||
Free(p);
|
||||
|
||||
return pNew;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// Base template class to support string conversion with memory sanitization after use
|
||||
///
|
||||
template<int t_nBufferLength = 128>
|
||||
class CW2AParanoidEX : public CW2AEX<t_nBufferLength> {
|
||||
public:
|
||||
///
|
||||
/// Initializes a new class instance with the string provided.
|
||||
///
|
||||
/// \param[in] psz Pointer to wide string
|
||||
///
|
||||
CW2AParanoidEX(_In_z_ LPCWSTR psz) throw(...) : CW2AEX<t_nBufferLength>(psz) {}
|
||||
|
||||
///
|
||||
/// Initializes a new class instance with the string provided.
|
||||
///
|
||||
/// \param[in] psz Pointer to wide string
|
||||
/// \param[in] nCodePage Code page to use when converting to single-byte string
|
||||
///
|
||||
CW2AParanoidEX(_In_z_ LPCWSTR psz, _In_ UINT nCodePage) throw(...) : CW2AEX<t_nBufferLength>(psz, nCodePage) {}
|
||||
|
||||
///
|
||||
/// Sanitizes string memory, then destroys it
|
||||
///
|
||||
~CW2AParanoidEX()
|
||||
{
|
||||
// Sanitize before free.
|
||||
if (m_psz != m_szBuffer)
|
||||
SecureZeroMemory(m_psz, _msize(m_psz));
|
||||
else
|
||||
SecureZeroMemory(m_szBuffer, sizeof(m_szBuffer));
|
||||
}
|
||||
};
|
||||
|
||||
///
|
||||
/// Support for string conversion with memory sanitization after use
|
||||
///
|
||||
typedef CW2AParanoidEX<> CW2AParanoid;
|
||||
|
||||
/// @}
|
||||
}
|
||||
-326
@@ -1,326 +0,0 @@
|
||||
/*
|
||||
Copyright 1991-2017 Amebis
|
||||
|
||||
This file is part of atlex.
|
||||
|
||||
atlex is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
atlex is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with atlex. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "atlex.h"
|
||||
#include <atlcoll.h>
|
||||
#include <atlstr.h>
|
||||
#include <MsiQuery.h>
|
||||
|
||||
///
|
||||
/// \defgroup ATLMSIAPI Microsoft Installer API
|
||||
/// Integrates ATL classes with Microsoft Installer API
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Gets the value for an installer property and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [MsiGetProperty function](https://msdn.microsoft.com/en-us/library/aa370134.aspx)
|
||||
///
|
||||
inline UINT MsiGetPropertyA(_In_ MSIHANDLE hInstall, _In_ LPCSTR szName, _Out_ ATL::CAtlStringA &sValue)
|
||||
{
|
||||
CHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(CHAR)];
|
||||
DWORD dwSize = _countof(szStackBuffer);
|
||||
UINT uiResult;
|
||||
|
||||
// Try with stack buffer first.
|
||||
uiResult = ::MsiGetPropertyA(hInstall, szName, szStackBuffer, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
memcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwSize);
|
||||
return NO_ERROR;
|
||||
} else if (uiResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap to read the string data into and read it.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize++);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
uiResult = ::MsiGetPropertyA(hInstall, szName, szBuffer, &dwSize);
|
||||
sValue.ReleaseBuffer(uiResult == NO_ERROR ? dwSize : 0);
|
||||
return uiResult;
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Gets the value for an installer property and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [MsiGetProperty function](https://msdn.microsoft.com/en-us/library/aa370134.aspx)
|
||||
///
|
||||
inline UINT MsiGetPropertyW(_In_ MSIHANDLE hInstall, _In_ LPCWSTR szName, _Out_ ATL::CAtlStringW &sValue)
|
||||
{
|
||||
WCHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(WCHAR)];
|
||||
DWORD dwSize = _countof(szStackBuffer);
|
||||
UINT uiResult;
|
||||
|
||||
// Try with stack buffer first.
|
||||
uiResult = ::MsiGetPropertyW(hInstall, szName, szStackBuffer, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
wmemcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwSize);
|
||||
return NO_ERROR;
|
||||
} else if (uiResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap to read the string data into and read it.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize++);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
uiResult = ::MsiGetPropertyW(hInstall, szName, szBuffer, &dwSize);
|
||||
sValue.ReleaseBuffer(uiResult == NO_ERROR ? dwSize : 0);
|
||||
return uiResult;
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns the string value of a record field and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [MsiRecordGetString function](https://msdn.microsoft.com/en-us/library/aa370368.aspx)
|
||||
///
|
||||
inline UINT MsiRecordGetStringA(_In_ MSIHANDLE hRecord, _In_ unsigned int iField, _Out_ ATL::CAtlStringA &sValue)
|
||||
{
|
||||
CHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(CHAR)];
|
||||
DWORD dwSize = _countof(szStackBuffer);
|
||||
UINT uiResult;
|
||||
|
||||
// Try with stack buffer first.
|
||||
uiResult = ::MsiRecordGetStringA(hRecord, iField, szStackBuffer, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
memcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwSize);
|
||||
return NO_ERROR;
|
||||
} else if (uiResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap to read the string data into and read it.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize++);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
uiResult = ::MsiRecordGetStringA(hRecord, iField, szBuffer, &dwSize);
|
||||
sValue.ReleaseBuffer(uiResult == NO_ERROR ? dwSize : 0);
|
||||
return uiResult;
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns the string value of a record field and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [MsiRecordGetString function](https://msdn.microsoft.com/en-us/library/aa370368.aspx)
|
||||
///
|
||||
inline UINT MsiRecordGetStringW(_In_ MSIHANDLE hRecord, _In_ unsigned int iField, _Out_ ATL::CAtlStringW &sValue)
|
||||
{
|
||||
WCHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(WCHAR)];
|
||||
DWORD dwSize = _countof(szStackBuffer);
|
||||
UINT uiResult;
|
||||
|
||||
// Try with stack buffer first.
|
||||
uiResult = ::MsiRecordGetStringW(hRecord, iField, szStackBuffer, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
wmemcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwSize);
|
||||
return NO_ERROR;
|
||||
} else if (uiResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap to read the string data into and read it.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize++);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
uiResult = ::MsiRecordGetStringW(hRecord, iField, szBuffer, &dwSize);
|
||||
sValue.ReleaseBuffer(uiResult == NO_ERROR ? dwSize : 0);
|
||||
return uiResult;
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Formats record field data and properties using a format string and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [MsiFormatRecord function](https://msdn.microsoft.com/en-us/library/aa370109.aspx)
|
||||
///
|
||||
inline UINT MsiFormatRecordA(MSIHANDLE hInstall, MSIHANDLE hRecord, ATL::CAtlStringA &sValue)
|
||||
{
|
||||
CHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(CHAR)];
|
||||
DWORD dwSize = _countof(szStackBuffer);
|
||||
UINT uiResult;
|
||||
|
||||
// Try with stack buffer first.
|
||||
uiResult = ::MsiFormatRecordA(hInstall, hRecord, szStackBuffer, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
memcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwSize);
|
||||
return NO_ERROR;
|
||||
} else if (uiResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap to format the string data into and read it.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize++);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
uiResult = ::MsiFormatRecordA(hInstall, hRecord, szBuffer, &dwSize);
|
||||
sValue.ReleaseBuffer(uiResult == NO_ERROR ? dwSize : 0);
|
||||
return uiResult;
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Formats record field data and properties using a format string and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [MsiFormatRecord function](https://msdn.microsoft.com/en-us/library/aa370109.aspx)
|
||||
///
|
||||
inline UINT MsiFormatRecordW(MSIHANDLE hInstall, MSIHANDLE hRecord, ATL::CAtlStringW &sValue)
|
||||
{
|
||||
WCHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(WCHAR)];
|
||||
DWORD dwSize = _countof(szStackBuffer);
|
||||
UINT uiResult;
|
||||
|
||||
// Try with stack buffer first.
|
||||
uiResult = ::MsiFormatRecordW(hInstall, hRecord, szStackBuffer, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
wmemcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwSize);
|
||||
return NO_ERROR;
|
||||
} else if (uiResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap to format the string data into and read it.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize++);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
uiResult = ::MsiFormatRecordW(hInstall, hRecord, szBuffer, &dwSize);
|
||||
sValue.ReleaseBuffer(uiResult == NO_ERROR ? dwSize : 0);
|
||||
return uiResult;
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Reads bytes from a record stream field into a ATL::CAtlArray<BYTE> buffer.
|
||||
///
|
||||
/// \sa [MsiRecordReadStream function](https://msdn.microsoft.com/en-us/library/aa370370.aspx)
|
||||
///
|
||||
inline UINT MsiRecordReadStream(_In_ MSIHANDLE hRecord, _In_ unsigned int iField, _Out_ ATL::CAtlArray<BYTE> &binData)
|
||||
{
|
||||
DWORD dwSize = 0;
|
||||
UINT uiResult;
|
||||
|
||||
// Query the actual data length first.
|
||||
uiResult = ::MsiRecordReadStream(hRecord, iField, NULL, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
if (!binData.SetCount(dwSize)) return ERROR_OUTOFMEMORY;
|
||||
return ::MsiRecordReadStream(hRecord, iField, (char*)binData.GetData(), &dwSize);
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns the full target path for a folder in the Directory table and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [MsiGetTargetPath function](https://msdn.microsoft.com/en-us/library/aa370303.aspx)
|
||||
///
|
||||
inline UINT MsiGetTargetPathA(_In_ MSIHANDLE hInstall, _In_ LPCSTR szFolder, _Out_ ATL::CAtlStringA &sValue)
|
||||
{
|
||||
CHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(CHAR)];
|
||||
DWORD dwSize = _countof(szStackBuffer);
|
||||
UINT uiResult;
|
||||
|
||||
// Try with stack buffer first.
|
||||
uiResult = ::MsiGetTargetPathA(hInstall, szFolder, szStackBuffer, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
memcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwSize);
|
||||
return NO_ERROR;
|
||||
} else if (uiResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap to format the string data into and read it.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize++);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
uiResult = ::MsiGetTargetPathA(hInstall, szFolder, szBuffer, &dwSize);
|
||||
sValue.ReleaseBuffer(uiResult == NO_ERROR ? dwSize : 0);
|
||||
return uiResult;
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns the full target path for a folder in the Directory table and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [MsiGetTargetPath function](https://msdn.microsoft.com/en-us/library/aa370303.aspx)
|
||||
///
|
||||
inline UINT MsiGetTargetPathW(_In_ MSIHANDLE hInstall, _In_ LPCWSTR szFolder, _Out_ ATL::CAtlStringW &sValue)
|
||||
{
|
||||
WCHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(WCHAR)];
|
||||
DWORD dwSize = _countof(szStackBuffer);
|
||||
UINT uiResult;
|
||||
|
||||
// Try with stack buffer first.
|
||||
uiResult = ::MsiGetTargetPathW(hInstall, szFolder, szStackBuffer, &dwSize);
|
||||
if (uiResult == NO_ERROR) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
wmemcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwSize);
|
||||
return NO_ERROR;
|
||||
} else if (uiResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap to format the string data into and read it.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize++);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
uiResult = ::MsiGetTargetPathW(hInstall, szFolder, szBuffer, &dwSize);
|
||||
sValue.ReleaseBuffer(uiResult == NO_ERROR ? dwSize : 0);
|
||||
return uiResult;
|
||||
} else {
|
||||
// Return error code.
|
||||
return uiResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// @}
|
||||
-117
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
Copyright 1991-2017 Amebis
|
||||
|
||||
This file is part of atlex.
|
||||
|
||||
atlex is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
atlex is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with atlex. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Security.h>
|
||||
|
||||
///
|
||||
/// \defgroup ATLSecurityAPI Security API
|
||||
/// Integrates ATL classes with Microsoft Security API
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Retrieves the name of the user or other security principal associated with the calling thread and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [GetUserNameEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx)
|
||||
///
|
||||
BOOLEAN GetUserNameExA(_In_ EXTENDED_NAME_FORMAT NameFormat, _Out_ ATL::CAtlStringA &sName)
|
||||
{
|
||||
CHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(CHAR)];
|
||||
ULONG ulSize = _countof(szStackBuffer);
|
||||
|
||||
// Try with stack buffer first.
|
||||
if (::GetUserNameExA(NameFormat, szStackBuffer, &ulSize)) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPSTR szBuffer = sName.GetBuffer(ulSize);
|
||||
if (!szBuffer) {
|
||||
SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
memcpy(szBuffer, szStackBuffer, ulSize);
|
||||
sName.ReleaseBuffer(ulSize);
|
||||
return TRUE;
|
||||
} else {
|
||||
if (::GetLastError() == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap and retry.
|
||||
LPSTR szBuffer = sName.GetBuffer(ulSize - 1);
|
||||
if (!szBuffer) {
|
||||
SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
if (::GetUserNameExA(NameFormat, szBuffer, &ulSize)) {
|
||||
sName.ReleaseBuffer(ulSize);
|
||||
return TRUE;
|
||||
} else {
|
||||
sName.ReleaseBuffer(0);
|
||||
return FALSE;
|
||||
}
|
||||
} else {
|
||||
// Return error.
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Retrieves the name of the user or other security principal associated with the calling thread and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [GetUserNameEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx)
|
||||
///
|
||||
BOOLEAN GetUserNameExW(_In_ EXTENDED_NAME_FORMAT NameFormat, _Out_ ATL::CAtlStringW &sName)
|
||||
{
|
||||
WCHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(WCHAR)];
|
||||
ULONG ulSize = _countof(szStackBuffer);
|
||||
|
||||
// Try with stack buffer first.
|
||||
if (::GetUserNameExW(NameFormat, szStackBuffer, &ulSize)) {
|
||||
// Allocate buffer on heap, copy from stack buffer, and zero terminate.
|
||||
LPWSTR szBuffer = sName.GetBuffer(ulSize);
|
||||
if (!szBuffer) {
|
||||
SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
wmemcpy(szBuffer, szStackBuffer, ulSize);
|
||||
sName.ReleaseBuffer(ulSize);
|
||||
return TRUE;
|
||||
} else {
|
||||
if (::GetLastError() == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap and retry.
|
||||
LPWSTR szBuffer = sName.GetBuffer(ulSize - 1);
|
||||
if (!szBuffer) {
|
||||
SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
if (::GetUserNameExW(NameFormat, szBuffer, &ulSize)) {
|
||||
sName.ReleaseBuffer(ulSize);
|
||||
return TRUE;
|
||||
} else {
|
||||
sName.ReleaseBuffer(0);
|
||||
return FALSE;
|
||||
}
|
||||
} else {
|
||||
// Return error.
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// @}
|
||||
-70
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
Copyright 1991-2017 Amebis
|
||||
|
||||
This file is part of atlex.
|
||||
|
||||
atlex is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
atlex is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with atlex. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atlstr.h>
|
||||
#include <Shlwapi.h>
|
||||
|
||||
///
|
||||
/// \defgroup ATLShellWAPI Shell API
|
||||
/// Integrates ATL classes with Microsoft Shell API
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Simplifies a path by removing navigation elements such as "." and ".." to produce a direct, well-formed path, and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [PathCanonicalize function](https://msdn.microsoft.com/en-us/library/windows/desktop/bb773569.aspx)
|
||||
///
|
||||
inline BOOL PathCanonicalizeA(__out ATL::CAtlStringA &sValue, __in LPCSTR pszPath)
|
||||
{
|
||||
// Allocate buffer on heap and read into it.
|
||||
LPSTR szBuffer = sValue.GetBuffer(MAX_PATH);
|
||||
if (!szBuffer) {
|
||||
::SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
BOOL bResult = ::PathCanonicalizeA(szBuffer, pszPath);
|
||||
sValue.ReleaseBuffer(bResult ? (int)strnlen(szBuffer, MAX_PATH) : 0);
|
||||
sValue.FreeExtra();
|
||||
return bResult;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Simplifies a path by removing navigation elements such as "." and ".." to produce a direct, well-formed path, and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [PathCanonicalize function](https://msdn.microsoft.com/en-us/library/windows/desktop/bb773569.aspx)
|
||||
///
|
||||
inline BOOL PathCanonicalizeW(__out ATL::CAtlStringW &sValue, __in LPCWSTR pszPath)
|
||||
{
|
||||
// Allocate buffer on heap and read into it.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(MAX_PATH);
|
||||
if (!szBuffer) {
|
||||
::SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
BOOL bResult = ::PathCanonicalizeW(szBuffer, pszPath);
|
||||
sValue.ReleaseBuffer(bResult ? (int)wcsnlen(szBuffer, MAX_PATH) : 0);
|
||||
sValue.FreeExtra();
|
||||
return bResult;
|
||||
}
|
||||
|
||||
/// @}
|
||||
-623
@@ -1,623 +0,0 @@
|
||||
/*
|
||||
Copyright 1991-2017 Amebis
|
||||
|
||||
This file is part of atlex.
|
||||
|
||||
atlex is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
atlex is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with atlex. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "atlex.h"
|
||||
#include <atlcoll.h>
|
||||
#include <atlstr.h>
|
||||
#include <Windows.h>
|
||||
|
||||
///
|
||||
/// \defgroup ATLWinAPI Windows API
|
||||
/// Integrates ATL classes with Microsoft Windows API
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Retrieves the fully qualified path for the file that contains the specified module and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [GetModuleFileName function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197.aspx)
|
||||
///
|
||||
inline DWORD GetModuleFileNameA(_In_opt_ HMODULE hModule, _Out_ ATL::CAtlStringA &sValue)
|
||||
{
|
||||
DWORD dwSize = 0;
|
||||
|
||||
for (;;) {
|
||||
// Increment size and allocate buffer.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize += 1024);
|
||||
if (!szBuffer) {
|
||||
::SetLastError(ERROR_OUTOFMEMORY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Try!
|
||||
DWORD dwResult = ::GetModuleFileNameA(hModule, szBuffer, dwSize);
|
||||
if (dwResult == 0) {
|
||||
// Error.
|
||||
sValue.ReleaseBuffer(0);
|
||||
return 0;
|
||||
} else if (dwResult < dwSize) {
|
||||
DWORD dwLength = (DWORD)strnlen(szBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwLength++);
|
||||
if (dwLength == dwSize) {
|
||||
// Buffer was long exactly enough.
|
||||
return dwResult;
|
||||
} if (dwLength < dwSize) {
|
||||
// Buffer was long enough to get entire string, and has some extra space left.
|
||||
sValue.FreeExtra();
|
||||
return dwResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Retrieves the fully qualified path for the file that contains the specified module and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [GetModuleFileName function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197.aspx)
|
||||
///
|
||||
inline DWORD GetModuleFileNameW(_In_opt_ HMODULE hModule, _Out_ ATL::CAtlStringW &sValue)
|
||||
{
|
||||
DWORD dwSize = 0;
|
||||
|
||||
for (;;) {
|
||||
// Increment size and allocate buffer.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize += 1024);
|
||||
if (!szBuffer) {
|
||||
::SetLastError(ERROR_OUTOFMEMORY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Try!
|
||||
DWORD dwResult = ::GetModuleFileNameW(hModule, szBuffer, dwSize);
|
||||
if (dwResult == 0) {
|
||||
// Error.
|
||||
sValue.ReleaseBuffer(0);
|
||||
return 0;
|
||||
} else if (dwResult < dwSize) {
|
||||
DWORD dwLength = (DWORD)wcsnlen(szBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwLength++);
|
||||
if (dwLength == dwSize) {
|
||||
// Buffer was long exactly enough.
|
||||
return dwResult;
|
||||
} if (dwLength < dwSize) {
|
||||
// Buffer was long enough to get entire string, and has some extra space left.
|
||||
sValue.FreeExtra();
|
||||
return dwResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Copies the text of the specified window's title bar (if it has one) into a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [GetWindowText function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520.aspx)
|
||||
///
|
||||
inline int GetWindowTextA(_In_ HWND hWnd, _Out_ ATL::CAtlStringA &sValue)
|
||||
{
|
||||
int iResult;
|
||||
|
||||
// Query the final string length first.
|
||||
iResult = ::GetWindowTextLengthA(hWnd);
|
||||
if (iResult > 0) {
|
||||
// Allocate buffer on heap and read the string data into it.
|
||||
LPSTR szBuffer = sValue.GetBuffer(iResult++);
|
||||
if (!szBuffer) {
|
||||
SetLastError(ERROR_OUTOFMEMORY);
|
||||
return 0;
|
||||
}
|
||||
iResult = ::GetWindowTextA(hWnd, szBuffer, iResult);
|
||||
sValue.ReleaseBuffer(iResult);
|
||||
return iResult;
|
||||
} else {
|
||||
// The result is empty.
|
||||
sValue.Empty();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Copies the text of the specified window's title bar (if it has one) into a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [GetWindowText function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520.aspx)
|
||||
///
|
||||
inline int GetWindowTextW(_In_ HWND hWnd, _Out_ ATL::CAtlStringW &sValue)
|
||||
{
|
||||
int iResult;
|
||||
|
||||
// Query the final string length first.
|
||||
iResult = ::GetWindowTextLengthW(hWnd);
|
||||
if (iResult > 0) {
|
||||
// Allocate buffer on heap and read the string data into it.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(iResult++);
|
||||
if (!szBuffer) {
|
||||
SetLastError(ERROR_OUTOFMEMORY);
|
||||
return 0;
|
||||
}
|
||||
iResult = ::GetWindowTextW(hWnd, szBuffer, iResult);
|
||||
sValue.ReleaseBuffer(iResult);
|
||||
return iResult;
|
||||
} else {
|
||||
// The result is empty.
|
||||
sValue.Empty();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Retrieves version information for the specified file and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [GetFileVersionInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms647003.aspx)
|
||||
///
|
||||
inline BOOL GetFileVersionInfoA(_In_ LPCSTR lptstrFilename, __reserved DWORD dwHandle, _Out_ ATL::CAtlArray<BYTE> &aValue)
|
||||
{
|
||||
// Get version info size.
|
||||
DWORD dwVerInfoSize = ::GetFileVersionInfoSizeA(lptstrFilename, &dwHandle);
|
||||
if (dwVerInfoSize != 0) {
|
||||
if (aValue.SetCount(dwVerInfoSize)) {
|
||||
// Read version info.
|
||||
return ::GetFileVersionInfoA(lptstrFilename, dwHandle, dwVerInfoSize, aValue.GetData());
|
||||
} else {
|
||||
::SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Retrieves version information for the specified file and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [GetFileVersionInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms647003.aspx)
|
||||
///
|
||||
inline BOOL GetFileVersionInfoW(_In_ LPCWSTR lptstrFilename, __reserved DWORD dwHandle, _Out_ ATL::CAtlArray<BYTE> &aValue)
|
||||
{
|
||||
// Get version info size.
|
||||
DWORD dwVerInfoSize = ::GetFileVersionInfoSizeW(lptstrFilename, &dwHandle);
|
||||
if (dwVerInfoSize != 0) {
|
||||
if (aValue.SetCount(dwVerInfoSize)) {
|
||||
// Read version info.
|
||||
return ::GetFileVersionInfoW(lptstrFilename, dwHandle, dwVerInfoSize, aValue.GetData());
|
||||
} else {
|
||||
::SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Expands environment-variable strings, replaces them with the values defined for the current user, and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [ExpandEnvironmentStrings function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724265.aspx)
|
||||
///
|
||||
inline DWORD ExpandEnvironmentStringsA(_In_ LPCSTR lpSrc, ATL::CAtlStringA &sValue)
|
||||
{
|
||||
DWORD dwBufferSizeEst = (DWORD)strlen(lpSrc) + 0x100; // Initial estimate
|
||||
|
||||
for (;;) {
|
||||
DWORD dwBufferSize = dwBufferSizeEst;
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwBufferSize);
|
||||
if (!szBuffer) {
|
||||
::SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
dwBufferSizeEst = ::ExpandEnvironmentStringsA(lpSrc, szBuffer, dwBufferSize);
|
||||
if (dwBufferSizeEst > dwBufferSize) {
|
||||
// The buffer was to small. Repeat with a bigger one.
|
||||
sValue.ReleaseBuffer(0);
|
||||
} else if (dwBufferSizeEst == 0) {
|
||||
// Error.
|
||||
sValue.ReleaseBuffer(0);
|
||||
return 0;
|
||||
} else {
|
||||
// The buffer was sufficient. Break.
|
||||
sValue.ReleaseBuffer();
|
||||
sValue.FreeExtra();
|
||||
return dwBufferSizeEst;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Expands environment-variable strings, replaces them with the values defined for the current user, and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [ExpandEnvironmentStrings function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724265.aspx)
|
||||
///
|
||||
inline DWORD ExpandEnvironmentStringsW(_In_ LPCWSTR lpSrc, ATL::CAtlStringW &sValue)
|
||||
{
|
||||
DWORD dwBufferSizeEst = (DWORD)wcslen(lpSrc) + 0x100; // Initial estimate
|
||||
|
||||
for (;;) {
|
||||
DWORD dwBufferSize = dwBufferSizeEst;
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwBufferSize);
|
||||
if (!szBuffer) {
|
||||
::SetLastError(ERROR_OUTOFMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
dwBufferSizeEst = ::ExpandEnvironmentStringsW(lpSrc, szBuffer, dwBufferSize);
|
||||
if (dwBufferSizeEst > dwBufferSize) {
|
||||
// The buffer was to small. Repeat with a bigger one.
|
||||
sValue.ReleaseBuffer(0);
|
||||
} else if (dwBufferSizeEst == 0) {
|
||||
// Error.
|
||||
sValue.ReleaseBuffer(0);
|
||||
return 0;
|
||||
} else {
|
||||
// The buffer was sufficient. Break.
|
||||
sValue.ReleaseBuffer();
|
||||
sValue.FreeExtra();
|
||||
return dwBufferSizeEst;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Formats GUID and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \param[in] lpGuid Pointer to GUID
|
||||
/// \param[out] str String to store the result to
|
||||
///
|
||||
inline VOID GuidToString(_In_ LPCGUID lpGuid, _Out_ ATL::CAtlStringA &str)
|
||||
{
|
||||
str.Format("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
|
||||
lpGuid->Data1,
|
||||
lpGuid->Data2,
|
||||
lpGuid->Data3,
|
||||
lpGuid->Data4[0], lpGuid->Data4[1],
|
||||
lpGuid->Data4[2], lpGuid->Data4[3], lpGuid->Data4[4], lpGuid->Data4[5], lpGuid->Data4[6], lpGuid->Data4[7]);
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Formats GUID and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \param[in] lpGuid Pointer to GUID
|
||||
/// \param[out] str String to store the result to
|
||||
///
|
||||
inline VOID GuidToString(_In_ LPCGUID lpGuid, _Out_ ATL::CAtlStringW &str)
|
||||
{
|
||||
str.Format(L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
|
||||
lpGuid->Data1,
|
||||
lpGuid->Data2,
|
||||
lpGuid->Data3,
|
||||
lpGuid->Data4[0], lpGuid->Data4[1],
|
||||
lpGuid->Data4[2], lpGuid->Data4[3], lpGuid->Data4[4], lpGuid->Data4[5], lpGuid->Data4[6], lpGuid->Data4[7]);
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Queries for a string value in the registry and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// `REG_EXPAND_SZ` are expanded using `ExpandEnvironmentStrings()` before storing to sValue.
|
||||
///
|
||||
/// \param[in] hReg A handle to an open registry key. The key must have been opened with the KEY_QUERY_VALUE access right.
|
||||
/// \param[in] pszName The name of the registry value. If lpValueName is NULL or an empty string, "", the function retrieves the type and data for the key's unnamed or default value, if any.
|
||||
/// \param[out] sValue String to store the value to
|
||||
/// \return
|
||||
/// - `ERROR_SUCCESS` when query succeeds;
|
||||
/// - `ERROR_INVALID_DATA` when the registy value type is not `REG_SZ`, `REG_MULTI_SZ`, or `REG_EXPAND_SZ`;
|
||||
/// - `ERROR_OUTOFMEMORY` when the memory allocation for the sValue buffer fails;
|
||||
/// - Error code when query fails. See `RegQueryValueEx()` for the list of error codes.
|
||||
/// \sa [RegQueryValueEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724911.aspx)
|
||||
/// \sa [ExpandEnvironmentStrings function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724265.aspx)
|
||||
///
|
||||
inline LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCSTR pszName, _Out_ ATL::CAtlStringA &sValue)
|
||||
{
|
||||
LSTATUS lResult;
|
||||
BYTE aStackBuffer[ATL_STACK_BUFFER_BYTES];
|
||||
DWORD dwSize = sizeof(aStackBuffer), dwType;
|
||||
|
||||
// Try with stack buffer first.
|
||||
lResult = ::RegQueryValueExA(hReg, pszName, NULL, &dwType, aStackBuffer, &dwSize);
|
||||
if (lResult == ERROR_SUCCESS) {
|
||||
if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
|
||||
// The value is REG_SZ or REG_MULTI_SZ. Allocate buffer on heap, copy from stack buffer.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize / sizeof(CHAR));
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
memcpy(szBuffer, aStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer();
|
||||
} else if (dwType == REG_EXPAND_SZ) {
|
||||
// The value is REG_EXPAND_SZ. Expand it from stack buffer.
|
||||
if (::ExpandEnvironmentStringsA((const CHAR*)aStackBuffer, sValue) == 0)
|
||||
lResult = ::GetLastError();
|
||||
} else {
|
||||
// The value is not a string type.
|
||||
lResult = ERROR_INVALID_DATA;
|
||||
}
|
||||
} else if (lResult == ERROR_MORE_DATA) {
|
||||
if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
|
||||
// The value is REG_SZ or REG_MULTI_SZ. Read it now.
|
||||
LPSTR szBuffer = sValue.GetBuffer(dwSize / sizeof(CHAR));
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL, (LPBYTE)szBuffer, &dwSize)) == ERROR_SUCCESS) {
|
||||
sValue.ReleaseBuffer();
|
||||
} else {
|
||||
// Reading of the value failed.
|
||||
sValue.ReleaseBuffer(0);
|
||||
}
|
||||
} else if (dwType == REG_EXPAND_SZ) {
|
||||
// The value is REG_EXPAND_SZ. Read it and expand environment variables.
|
||||
ATL::CTempBuffer<CHAR> sTemp(dwSize / sizeof(CHAR));
|
||||
if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL, (LPBYTE)(CHAR*)sTemp, &dwSize)) == ERROR_SUCCESS)
|
||||
if (::ExpandEnvironmentStringsA((const CHAR*)sTemp, sValue) == 0)
|
||||
lResult = ::GetLastError();
|
||||
} else {
|
||||
// The value is not a string type.
|
||||
lResult = ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Queries for a string value in the registry and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// `REG_EXPAND_SZ` are expanded using `ExpandEnvironmentStrings()` before storing to sValue.
|
||||
///
|
||||
/// \param[in] hReg A handle to an open registry key. The key must have been opened with the KEY_QUERY_VALUE access right.
|
||||
/// \param[in] pszName The name of the registry value. If lpValueName is NULL or an empty string, "", the function retrieves the type and data for the key's unnamed or default value, if any.
|
||||
/// \param[out] sValue String to store the value to
|
||||
/// \return
|
||||
/// - `ERROR_SUCCESS` when query succeeds;
|
||||
/// - `ERROR_INVALID_DATA` when the registy value type is not `REG_SZ`, `REG_MULTI_SZ`, or `REG_EXPAND_SZ`;
|
||||
/// - `ERROR_OUTOFMEMORY` when the memory allocation for the sValue buffer fails;
|
||||
/// - Error code when query fails. See `RegQueryValueEx()` for the list of error codes.
|
||||
/// \sa [RegQueryValueEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724911.aspx)
|
||||
/// \sa [ExpandEnvironmentStrings function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724265.aspx)
|
||||
///
|
||||
inline LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCWSTR pszName, _Out_ ATL::CAtlStringW &sValue)
|
||||
{
|
||||
LSTATUS lResult;
|
||||
BYTE aStackBuffer[ATL_STACK_BUFFER_BYTES];
|
||||
DWORD dwSize = sizeof(aStackBuffer), dwType;
|
||||
|
||||
// Try with stack buffer first.
|
||||
lResult = ::RegQueryValueExW(hReg, pszName, NULL, &dwType, aStackBuffer, &dwSize);
|
||||
if (lResult == ERROR_SUCCESS) {
|
||||
if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
|
||||
// The value is REG_SZ or REG_MULTI_SZ. Allocate buffer on heap, copy from stack buffer.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize / sizeof(WCHAR));
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
memcpy(szBuffer, aStackBuffer, dwSize);
|
||||
sValue.ReleaseBuffer();
|
||||
} else if (dwType == REG_EXPAND_SZ) {
|
||||
// The value is REG_EXPAND_SZ. Expand it from stack buffer.
|
||||
if (::ExpandEnvironmentStringsW((const WCHAR*)aStackBuffer, sValue) == 0)
|
||||
lResult = ::GetLastError();
|
||||
} else {
|
||||
// The value is not a string type.
|
||||
lResult = ERROR_INVALID_DATA;
|
||||
}
|
||||
} else if (lResult == ERROR_MORE_DATA) {
|
||||
if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
|
||||
// The value is REG_SZ or REG_MULTI_SZ. Read it now.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize / sizeof(WCHAR));
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL, (LPBYTE)szBuffer, &dwSize)) == ERROR_SUCCESS) {
|
||||
sValue.ReleaseBuffer();
|
||||
} else {
|
||||
// Reading of the value failed.
|
||||
sValue.ReleaseBuffer(0);
|
||||
}
|
||||
} else if (dwType == REG_EXPAND_SZ) {
|
||||
// The value is REG_EXPAND_SZ. Read it and expand environment variables.
|
||||
ATL::CTempBuffer<WCHAR> sTemp(dwSize / sizeof(WCHAR));
|
||||
if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL, (LPBYTE)(WCHAR*)sTemp, &dwSize)) == ERROR_SUCCESS)
|
||||
if (::ExpandEnvironmentStringsW((const WCHAR*)sTemp, sValue) == 0)
|
||||
lResult = ::GetLastError();
|
||||
} else {
|
||||
// The value is not a string type.
|
||||
lResult = ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Retrieves the type and data for the specified value name associated with an open registry key and stores the data in a ATL::CAtlArray<BYTE> buffer.
|
||||
///
|
||||
/// \sa [RegQueryValueEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724911.aspx)
|
||||
///
|
||||
inline LSTATUS RegQueryValueExA(_In_ HKEY hKey, _In_opt_ LPCSTR lpValueName, __reserved LPDWORD lpReserved, _Out_opt_ LPDWORD lpType, _Out_ ATL::CAtlArray<BYTE> &aData)
|
||||
{
|
||||
LSTATUS lResult;
|
||||
BYTE aStackBuffer[ATL_STACK_BUFFER_BYTES];
|
||||
DWORD dwSize = sizeof(aStackBuffer);
|
||||
|
||||
// Try with stack buffer first.
|
||||
lResult = RegQueryValueExA(hKey, lpValueName, lpReserved, NULL, aStackBuffer, &dwSize);
|
||||
if (lResult == ERROR_SUCCESS) {
|
||||
// Allocate buffer on heap, copy from stack buffer.
|
||||
if (!aData.SetCount(dwSize)) return ERROR_OUTOFMEMORY;
|
||||
memcpy(aData.GetData(), aStackBuffer, dwSize);
|
||||
} else if (lResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap and retry.
|
||||
if (!aData.SetCount(dwSize)) return ERROR_OUTOFMEMORY;
|
||||
if ((lResult = RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, aData.GetData(), &dwSize)) != ERROR_SUCCESS)
|
||||
aData.SetCount(0);
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Retrieves the type and data for the specified value name associated with an open registry key and stores the data in a ATL::CAtlArray<BYTE> buffer.
|
||||
///
|
||||
/// \sa [RegQueryValueEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724911.aspx)
|
||||
///
|
||||
inline LSTATUS RegQueryValueExW(_In_ HKEY hKey, _In_opt_ LPCWSTR lpValueName, __reserved LPDWORD lpReserved, _Out_opt_ LPDWORD lpType, _Out_ ATL::CAtlArray<BYTE> &aData)
|
||||
{
|
||||
LSTATUS lResult;
|
||||
BYTE aStackBuffer[ATL_STACK_BUFFER_BYTES];
|
||||
DWORD dwSize = sizeof(aStackBuffer);
|
||||
|
||||
// Try with stack buffer first.
|
||||
lResult = RegQueryValueExW(hKey, lpValueName, lpReserved, NULL, aStackBuffer, &dwSize);
|
||||
if (lResult == ERROR_SUCCESS) {
|
||||
// Allocate buffer on heap, copy from stack buffer.
|
||||
if (!aData.SetCount(dwSize)) return ERROR_OUTOFMEMORY;
|
||||
memcpy(aData.GetData(), aStackBuffer, dwSize);
|
||||
} else if (lResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap and retry.
|
||||
if (!aData.SetCount(dwSize)) return ERROR_OUTOFMEMORY;
|
||||
if ((lResult = RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, aData.GetData(), &dwSize)) != ERROR_SUCCESS)
|
||||
aData.SetCount(0);
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
|
||||
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
|
||||
|
||||
///
|
||||
/// Loads the specified string from the specified key and subkey, and stores it in a ATL::CAtlStringA string.
|
||||
///
|
||||
/// \sa [RegLoadMUIString function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724890.aspx)
|
||||
///
|
||||
inline LSTATUS RegLoadMUIStringA(_In_ HKEY hKey, _In_opt_ LPCSTR pszValue, _Out_ ATL::CAtlStringA &sOut, _In_ DWORD Flags, _In_opt_ LPCSTR pszDirectory)
|
||||
{
|
||||
LSTATUS lResult;
|
||||
CHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(CHAR)];
|
||||
DWORD dwSize;
|
||||
|
||||
Flags &= ~REG_MUI_STRING_TRUNCATE;
|
||||
|
||||
// Try with stack buffer first.
|
||||
lResult = RegLoadMUIStringA(hKey, pszValue, szStackBuffer, _countof(szStackBuffer), &dwSize, Flags, pszDirectory);
|
||||
if (lResult == ERROR_SUCCESS) {
|
||||
// Allocate buffer on heap, copy from stack buffer.
|
||||
LPSTR szBuffer = sOut.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
memcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sOut.ReleaseBuffer(dwSize);
|
||||
} else if (lResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap and retry.
|
||||
LPSTR szBuffer = sOut.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
sOut.ReleaseBuffer((lResult = RegLoadMUIStringA(hKey, pszValue, szBuffer, dwSize, &dwSize, Flags, pszDirectory)) == ERROR_SUCCESS ? dwSize : 0);
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Loads the specified string from the specified key and subkey, and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [RegLoadMUIString function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724890.aspx)
|
||||
///
|
||||
inline LSTATUS RegLoadMUIStringW(_In_ HKEY hKey, _In_opt_ LPCWSTR pszValue, _Out_ ATL::CAtlStringW &sOut, _In_ DWORD Flags, _In_opt_ LPCWSTR pszDirectory)
|
||||
{
|
||||
LSTATUS lResult;
|
||||
WCHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(WCHAR)];
|
||||
DWORD dwSize;
|
||||
|
||||
Flags &= ~REG_MUI_STRING_TRUNCATE;
|
||||
|
||||
// Try with stack buffer first.
|
||||
lResult = RegLoadMUIStringW(hKey, pszValue, szStackBuffer, _countof(szStackBuffer), &dwSize, Flags, pszDirectory);
|
||||
if (lResult == ERROR_SUCCESS) {
|
||||
// Allocate buffer on heap, copy from stack buffer.
|
||||
LPWSTR szBuffer = sOut.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
wmemcpy(szBuffer, szStackBuffer, dwSize);
|
||||
sOut.ReleaseBuffer(dwSize);
|
||||
} else if (lResult == ERROR_MORE_DATA) {
|
||||
// Allocate buffer on heap and retry.
|
||||
LPWSTR szBuffer = sOut.GetBuffer(dwSize);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
sOut.ReleaseBuffer((lResult = RegLoadMUIStringW(hKey, pszValue, szBuffer, dwSize, &dwSize, Flags, pszDirectory)) == ERROR_SUCCESS ? dwSize : 0);
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// @}
|
||||
|
||||
|
||||
namespace ATL
|
||||
{
|
||||
/// \addtogroup ATLWinAPI
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Module handle wrapper
|
||||
///
|
||||
class CAtlLibrary : public CObjectWithHandleT<HMODULE>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Frees the module.
|
||||
///
|
||||
/// \sa [FreeLibrary](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683152.aspx)
|
||||
///
|
||||
virtual ~CAtlLibrary()
|
||||
{
|
||||
if (m_h)
|
||||
FreeLibrary(m_h);
|
||||
}
|
||||
|
||||
///
|
||||
/// Loads the specified module into the address space of the calling process.
|
||||
///
|
||||
/// \sa [LoadLibraryEx](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179.aspx)
|
||||
///
|
||||
inline BOOL Load(_In_ LPCTSTR lpFileName, __reserved HANDLE hFile, _In_ DWORD dwFlags)
|
||||
{
|
||||
HANDLE h = LoadLibraryEx(lpFileName, hFile, dwFlags);
|
||||
if (h) {
|
||||
Attach(h);
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// Frees the module.
|
||||
///
|
||||
/// \sa [FreeLibrary](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683152.aspx)
|
||||
///
|
||||
virtual void InternalFree()
|
||||
{
|
||||
FreeLibrary(m_h);
|
||||
}
|
||||
};
|
||||
|
||||
/// @}
|
||||
}
|
||||
-79
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
Copyright 1991-2017 Amebis
|
||||
|
||||
This file is part of atlex.
|
||||
|
||||
atlex is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
atlex is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with atlex. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atlstr.h>
|
||||
#include <wlanapi.h>
|
||||
|
||||
|
||||
// Must not statically link to Wlanapi.dll as it is not available on Windows
|
||||
// without a WLAN interface.
|
||||
extern DWORD (WINAPI *pfnWlanReasonCodeToString)(__in DWORD dwReasonCode, __in DWORD dwBufferSize, __in_ecount(dwBufferSize) PWCHAR pStringBuffer, __reserved PVOID pReserved);
|
||||
|
||||
|
||||
///
|
||||
/// \defgroup ATLWLANAPI WLAN API
|
||||
/// Integrates ATL classes with Microsoft WLAN API
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Retrieves a string that describes a specified reason code and stores it in a ATL::CAtlStringW string.
|
||||
///
|
||||
/// \sa [WlanReasonCodeToString function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706768.aspx)
|
||||
///
|
||||
/// \note
|
||||
/// Since Wlanapi.dll is not always present, the \c pfnWlanReasonCodeToString pointer to \c WlanReasonCodeToString
|
||||
/// function must be loaded dynamically.
|
||||
///
|
||||
inline DWORD WlanReasonCodeToString(_In_ DWORD dwReasonCode, _Out_ ATL::CAtlStringW &sValue, __reserved PVOID pReserved)
|
||||
{
|
||||
DWORD dwSize = 0;
|
||||
|
||||
if (!::pfnWlanReasonCodeToString)
|
||||
return ERROR_CALL_NOT_IMPLEMENTED;
|
||||
|
||||
for (;;) {
|
||||
// Increment size and allocate buffer.
|
||||
LPWSTR szBuffer = sValue.GetBuffer(dwSize += 1024);
|
||||
if (!szBuffer) return ERROR_OUTOFMEMORY;
|
||||
|
||||
// Try!
|
||||
DWORD dwResult = ::pfnWlanReasonCodeToString(dwReasonCode, dwSize, szBuffer, pReserved);
|
||||
if (dwResult == NO_ERROR) {
|
||||
DWORD dwLength = (DWORD)wcsnlen(szBuffer, dwSize);
|
||||
sValue.ReleaseBuffer(dwLength++);
|
||||
if (dwLength == dwSize) {
|
||||
// Buffer was long exactly enough.
|
||||
return NO_ERROR;
|
||||
} else if (dwLength < dwSize) {
|
||||
// Buffer was long enough to get entire string, and has some extra space left.
|
||||
sValue.FreeExtra();
|
||||
return NO_ERROR;
|
||||
}
|
||||
} else {
|
||||
// Return error code.
|
||||
sValue.ReleaseBuffer(0);
|
||||
return dwResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// @}
|
||||
Reference in New Issue
Block a user