반응형

C Language String Types and Classes

String Type

Description

char/wchar/TCHAR The C strings for ANSI and Unicode
CString The C++/MFC class wrapper for C strings
BSTR The Visual Basic string type
_bstr_t A C++ class wrapper for the Visual Basic string type
CComBSTR Yet another C++ class wrapper for the Visual Basic string type used predominately in ATL code

 

Demo Code

 

BSTR to CString and CString to BSTR

 

void BSTRtoCString(BSTR bStr){
    CString csStr =bStr;
}

void CStringtoBSTR()
{
    CString csStr = NULL;

    csStr = "Hello BelieveIT!";

    BSTR bStr = csStr.AllocSysString();

    BSTRtoCString(bStr);

    ::SysFreeString(bStr); //finished using the BSTR

}

 

Base Code

BSTR GetBSTR()
{
    _bstr_t bstrt(_T("This is the test string!"));

    BSTR bstr;

    bstr = bstrt.copy();

    return bstr;
}

CComBSTR GetComBSTR()
{
    CComBSTR bstr("This is the test string!");

    return bstr;

}

void ShowBSTR(BSTR bstr)
{
    _bstr_t bstrt(bstr);

    CString cstr;

    cstr.Format(_T("%s"),(LPCTSTR)bstrt);

    AfxMessageBox(cstr);

}

 

Conversions Code

 

_bstr_t BSTRto_bstr_t()
{
    BSTR bstr = GetBSTR();

    _bstr_t bstrFinal(bstr);

    ShowBSTR(bstrFinal);

    bstrFinal = bstr;

    return bstrFinal;
}

 

BSTR _bstr_ttoBSTR()
{
    _bstr_t bstr(_T("This is Converting string"));

    BSTR bstrFinish;

    bstrFinish = bstr.copy();

    ShowBSTR(bstrFinish);

    bstrFinish = bstr;

    return bstrFinish;

}

 

BSTR CComBSTRtoBSTR()
{
    CComBSTR bstr(_T("This is Converting string"));

    BSTR bstrFinish;

    bstrFinish = bstr;

    ShowBSTR(bstrFinish);

    bstrFinish = bstr.Copy();

    return bstrFinish;

}

 

CComBSTR _bstr_ttoCComBSTR()
{
    _bstr_t bstr(_T("This is Converting string"));

    CComBSTR bstrFinish;

    bstrFinish.AppendBSTR(bstr);

    return bstrFinish;

}

 

CString BSTRtoCString()
{
    //conversion that only works in Unicode
    BSTR bstr;

    bstr = GetBSTR();

    TCHAR szFinal[255] = {0,};

    _stprintf(szFinal, _T("%s"), (LPCTSTR)bstr);

    AfxMessageBox(szFinal);

    _bstr_t bstrIntermediate(bstr);

    CString strFinal;

    _stprintf(szFinal, _T("%s"), (LPCTSTR)bstrIntermediate);

    strFinal.Format(_T("%s"),(LPCTSTR)bstrIntermediate);

    return strFinal;

}

 

CString _bstr_ttoCString()
{
    _bstr_t bstr(_T("This is Converting string"));

    TCHAR szFinal[255] = {0,};

    _stprintf(szFinal,_T("%s"),(LPCTSTR)bstr);

    return szFinal;
}

 

LPCTSTR CComBSTRtoLPCTSTR()
{

    CComBSTR bstr("This is Converting string");

    _bstr_t bstrIntermediate(bstr);

    TCHAR szFinal[255] = {0,};

    _stprintf(szFinal, _T("%s"),(LPCTSTR)bstrIntermediate);

    return szFinal;
}

 

_bstr_t LPCTSTRto_bstr_t()
{
    LPCTSTR szStart = _T("This is Converting string");

    _bstr_t bstrFinal(szStart);

    ShowBSTR(bstrFinal);

    bstrFinal = szStart;

    return bstrFinal;
}

 

CComBSTR LPCTSTRtoComBSTR()
{
    LPCTSTR szStart = _T("This is Converting string");

    CComBSTR bstrFinal(szStart);

    ShowBSTR(bstrFinal);

    bstrFinal.Empty();

    bstrFinal.Append(szStart);

    return bstrFinal;
}

 

[ Reference ]

http://blog.naver.com/mismir?Redirect=Log&logNo=40013446819
http://www.codeproject.com/Articles/1282/Easy-way-to-manipulate-BSTR-using-CString-class

반응형
반응형

 

MFC 프로그램 사용 중 간헐적으로 아래와 같은 "서버 작업 중" 대화 상자가 출력될 때가 있다.

 

서버 작업 중 

 

<요 약>

MFC로 작성된 ActiveX클라이언트 애플리케이션이 ActiveX서버를 사용하려 할 때 다음과 같은 대화 상자가

나타나는 경우가 있습니다.


서버 작업 중 – 다른 프로그램이 사용 중이므로 이 작업을 완료할 수 없습니다. [전환]을 선택하여 사용 중인

프로그램을 활성화하여 문제를 해결하십시오.

Component Request Pending – This Action cannot be completed because the other application is busy.

Choose 'switch to' to activate the busy application and correct the problem


이 대화상자는 ActiveX서버가 클라이언트 애플리케이션에게 응답을 하지 못할 때 나타나게 됩니다.

 

<현상>

MFC로 작성된 ActiveX 클라이언트 애플리케이션이 ActiveX 서버를 사용하려 할 때 다음과 같은 대화상자

가 나타나는 경우가 있습니다.


"서버 작업 중 - 다른 프로그램이 사용 중이므로 이 작업을 완료할 수 없습니다. [전환]을 선택하여 사용 중인

프로그램을 활성화하여 문제를 해결하십시오"

"Component Request Pending - This Action cannot be completed because the other application is busy.

Choose 'switch to' to activate the busy application and correct the problem"


이때 '전환'을 선택하고 기다리면 정상적으로 동작합니다.

 

<원인>

사용하고자 하는 ActiveX 컨트롤이 어떤 이유로 인해 응답하지 못하고 있는 상태입니다. 다음과 같은 이유에서

발생할 수 있습니다.

1.ActiveX 컨트롤이 초기화되기 전에 클라이언트 애플리케이션이 이를 사용하려고 시도할 때

2.시스템이 느리거나 동시에 많은 애플리케이션이 동작 중이어서 ActiveX 컨트롤이 응답하지 못하는 경우

3.ActiveX 컨트롤의 설계에 문제가 있어서 응답이 늦도록 제작되어 있을 때

 

<해결 방법>

첫번째 방법은 다음과 같이 CWinApp::InitInstance()의 파생 함수에서 ColeMessageFilter::SetMessagePendingDelay() 함수를

이용하여 ActiveX 컨트롤의 응답을 기다리는 시간을 길게 설정하면 됩니다.

 

BOOL CMyApp::InitInstance() {

// ...

AfxOleInit();

COleMessageFilter * pFilter = AfxOleGetMessageFilter();

pFilter->SetMessagePendingDelay(5000);

// ...

}

 

여기서 SetMessagePendingDelay()의 인자는 클라이언트 애플리케이션이 ActiveX 서버의 응답을 기다리는 시간으로 밀리세컨드 단위입니다.

 

두번째 방법은 응답을 기다리는 시간을 설정하는 것이 아니라 메시지 박스를 출력하지 않게 하는 설정 입니다.

 

BOOL CMyApp::InitInstance() {

// ...

AfxOleInit();

COleMessageFilter * pFilter = AfxOleGetMessageFilter();

pFilter->EnableBusyDialog(FALSE);

pFilter->EnableNotRespondingDialog(FALSE);

// ...

}

 

END

MS 참고 사이트 입니다. (http://support.microsoft.com/default.aspx?scid=kb%3Bko%3B602164)

반응형

+ Recent posts