Win32 API 를 이용한 FreeAndNil 과 같은 역할을 하는 Thread-safe 한 함수이다.

하지만, Reference counting 은 없기에 여러 변수에서 참조하는 주소라면 문제가 될 수 있다.

(만능이 아니라는 말이다)


template<class T>

void __fastcall SafeFreeAndNil(T **Obj)

{

delete (T*)InterlockedExchangePointer(*Obj, NULL);

}


function GetBytesPerCluster(const ADrive: String): Int64;

var

  SectorsPerCluster: DWORD;

  BytesPerSector: DWORD;

  Dummy: DWORD;

begin

  if GetDiskFreeSpace(PChar(ADrive), SectorsPerCluster, BytesPerSector, Dummy, Dummy) = 0 then

    Exit(0);

  Result := SectorsPerCluster * BytesPerSector;

end;



$A              Determines whether data is aligned or packed

$Align          Determines whether data is aligned or packed

$AppType        Determines the application type : GUI or Console

$B              Whether to short cut and and or operations

$BoolEval       Whether to short cut and and or operations

$D              Determines whether application debug information is built

$DebugInfo      Determines whether application debug information is built

$Define         Defines a compiler directive symbol - as used by IfDef

$DefinitionInfo Determines whether application symbol information is built

$Else           Starts the alternate section of an IfDef or IfNDef

$EndIf          Terminates conditional code compilation

$ExtendedSyntax Controls some Pascal extension handling

$H              Treat string types as AnsiString or ShortString

$Hints          Determines whether Delphi shows compilation hints

$I              Allows code in an include file to be incorporated into a Unit

$IfDef          Executes code if a conditional symbol has been defined

$IfNDef         Executes code if a conditional symbol has not been defined

$IfOpt          Tests for the state of a Compiler directive

$Include        Allows code in an include file to be incorporated into a Unit

$IOChecks       When on, an IO operation error throws an exception

$L              Determines what application debug information is built

$LocalSymbols   Determines what application debug information is built

$LongStrings    Treat string types as AnsiString or ShortString

$MinEnumSize    Sets the minimum storage used to hold enumerated types

$O              Determines whether Delphi optimises code when compiling

$Optimization   Determines whether Delphi optimises code when compiling

$OverFlowChecks Determines whether Delphi checks integer and enum bounds

$Q              Determines whether Delphi checks integer and enum bounds

$R              Determines whether Delphi checks array bounds

$RangeChecks    Determines whether Delphi checks array bounds

$ReferenceInfo  Determines whether symbol reference information is built

$Resource       Defines a resource file to be included in the application linking

$UnDef          Undefines a compiler directive symbol - as used by IfDef

$Warnings       Determines whether Delphi shows compilation warnings

$X              Controls some Pascal extension handling

$Y              Determines whether application symbol information is built

$Z              Sets the minimum storage used to hold enumerated types



C++Builder 2007 에서 'TClientSocket' 과 'TIdHTTP' 를 같이 사용하려할 때 발생했다.


'TClientSocket' 은 winsock v1 을 'TIdHTTP' 는 winsock v2 를 사용하여 발생하는 문제다.


인터넷을 열심히 검색해보면 비슷하지만 여러 해결책을 제시한다.


1. 'Conditional Defines' 에 '_WINSOCKAPI_' 추가


2. 다음 코드를 #include <windows.h> 이전에 추가

#ifndef _WINSOCKAPI_

#define _WINSOCKAPI_

#endif


3. 'winsock2.h' 를 'windows.h' 이전에 포함



어떤 방법이든 그대로만 하면 다 해결되지 않고 또 다른 에러에 빠져든다.


그런데 모든 방법은 winsock v1 을 사용하기 전에 winsock v2 를 포함하여 대체하라는 방향을 제시한다.

(winsock2.h 에는 _WINSOCKAPI_ 가 정의되어 winsock v1 의 코드를 배제시키게 되어있다.)


그런데 위의 해결책이 통하지 않는 이유는 어디선가에서 winsock v1 이 먼저 포함되었기 때문이다.


그곳은 'vcl.h' 인 것 같다.


다른 코드 수정없이 모든 cpp 파일에서 #include <vcl.h> 부분을 다음과 같이 'winsock2.h' 를 먼저 포함하게 바꾸니 해결되었다.


#include <winsock2.h>

#include <vcl.h>

#pragma hdrstop


'Windows > 문제해결' 카테고리의 다른 글

[RC Error] Invalid bitmap format  (0) 2009.08.13
[RAD] Process 종료 메세지  (0) 2008.04.03
[INI] 값 저장/참조에 있어 주의점  (0) 2007.06.04
[WIN32] SetWindowLong 이용시 객체 접근  (0) 2007.06.04
[CB] 주석문 \ 에러  (0) 2007.05.25

WDCC (WMI Delphi Code Creator) 를 이용하자.


http://theroadtodelphi.wordpress.com/wmi-delphi-code-creator/

'Windows > RAD Studio' 카테고리의 다른 글

[DEL] 논리드라이브 클러스터 크기 확인 (Win32)  (0) 2015.04.21
[Del] Compiler Directive  (0) 2014.12.08
[BCB] 관리자 권한 확인  (0) 2013.07.09
[BAT] 관리자 권한 확인  (0) 2013.07.09
enum 값 문자열 출력  (0) 2013.07.05


#include <sddl.h>

bool __fastcall IsAdministratorPrivilege(void)

{

DWORD dwSize;

HANDLE hToken;

PTOKEN_MANDATORY_LABEL ptml;

LPWSTR szSid;


::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &hToken);

::GetTokenInformation(hToken, TokenIntegrityLevel, NULL, 0, &dwSize);

ptml = (PTOKEN_MANDATORY_LABEL)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);

try {

::GetTokenInformation(hToken, TokenIntegrityLevel, ptml, dwSize, &dwSize);

::ConvertSidToStringSid(ptml->Label.Sid, &szSid);

try {

return (_wcscmpi(szSid, __TEXT("S-1-16-12288")) == 0);

} __finally {

::LocalFree(szSid);

}

} __finally {

::HeapFree(::GetProcessHeap(), 0, (LPVOID)ptml);

}

return false;

}



BCB6 에서는 PTOKEN_MANDATORY_LABEL 과 ToeknIntegrityLevel 이 없다.

다음 코드를 앞에 추가하면 잘 동작할 것이다.


typedef struct _TOKEN_MANDATORY_LABEL {

    SID_AND_ATTRIBUTES Label;

} TOKEN_MANDATORY_LABEL, *PTOKEN_MANDATORY_LABEL;

#define TokenIntegrityLevel ((TOKEN_INFORMATION_CLASS)25)



@ECHO OFF

AT > NUL

IF %ERRORLEVEL% EQU 0 (

  ECHO Administrator mode

) ELSE (

  ECHO User mode

)

Pause


enum Value {vlOne, vlTwo, vlThree};

Value value = 1;


이경우 value 의 값에 따라 "vlTwo" 가 필요한 경우가 있다.

이때 다음처럼 사용하면된다.


System::Typinfo::GetEnumName(__delphirtti(TType), Value);


__delphirtti 에 VCL 타입만 적용 가능한 하위버전의 개발툴에서는 다음과 같이 사용하면 된다.

(2007 까지는 VCL 타입만 가능한 것을 확인하였고, XE4 이후는 enum 도 가능한 것을 확인하였다. 그 사이 버전은 모르겠다.)


#define MakeEnumHelper(T, C) \

class C : public TObject { \

public: \

static String __fastcall GetEnumName(int V) { \

return Typinfo::GetEnumName(*Typinfo::GetPropInfo(__typeinfo(C), "P")->PropType, V);\

} \

__published: \

__property T P = {default = 0}; \

};


사용예)
MakeEnumHelper(TType, TTypeHelper);

ShowMessage(TTypeHelper::GetEnumName(Value));


form input[type=text], form input[type=password] {

  width: 60pt;

}

form input {

  width: expression(this.type == 'text' || this.type == 'password'? '60pt': '');

}



log10 함수를 이용해 숫자값에 대한 문자형태의 길이를 구할 수 있다.


(int)log10((double)iValue + 1) + 1


int iValue = 123;

int iLength = (int)log10((double)iValue + 1) + 1;


+ Recent posts