Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ATL COM Object GET property using BSTR

Posted on 2002-07-26
2
Medium Priority
?
377 Views
Last Modified: 2013-11-25
Is this code correct..IE will it product memory leaks?


STDMETHODIMP CUPS::get_m_strLastError(BSTR *pVal)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState())

    // TODO: Add your implementation code here
    SysFreeString(*pVal);
    CString strError;

    strError="<font style='font-size:10pt;font-family:Verdana,Arial;'>";
    strError+=m_strLastError+"</font>\0";

    int iSize=strError.GetLength();
    char* str = strError.GetBuffer(iSize);
    unsigned short *newstr= NULL;
   
    int iWideSize=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,newstr,0);

    newstr = new unsigned short[iWideSize];
    memset(newstr,0,iWideSize);

    MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,newstr,iWideSize);
    *pVal=SysAllocString(newstr);

    delete (unsigned short*)newstr;
    strError.ReleaseBuffer();
    return S_OK;
}
0
Comment
Question by:Dredwick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
migel earned 200 total points
ID: 7183276
Hi!
no your code will not lead to memory leak
but some additions:

// here you jave to use WCHAR
 newstr = new WCHAR[iWideSize]; //<<
   memset(newstr,0,iWideSize*sizeof(WCHAR));//<<

   MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,newstr,iWideSize);
   *pVal=SysAllocString(newstr);

   delete[] newstr; //<<<
   strError.ReleaseBuffer();
0
 

Author Comment

by:Dredwick
ID: 7183553
Thanks Migel!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question