Results 1 to 6 of 6

Thread: Crashing help!!! [RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Basingstoke
    Posts
    86

    Question Crashing help!!! [RESOLVED]

    The below code causes my VB code to crash with the ' The memory could not be "read" ' error.

    BSTR WINAPI FormatID(HWND hWnd, LPCSTR sLine)
    {
    char *tmp = new char[strlen(sLine)];
    int n;

    n = <to be calculated>;
    strcpy(tmp, sLine);
    return BSTR(tmp[n]);
    }

    What I am trying to do is get the n'th character from sLine.

    Any ideas, thanks in advanced.
    Last edited by DrHippyHomer; Dec 9th, 2002 at 04:41 AM.

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    BSTR WINAPI FormatID(HWND hWnd, LPCSTR sLine) 
    { 
    char *tmp = new char[strlen(sLine)]; <- local variable
    int n; 
    
    n = <to be calculated>; 
    strcpy(tmp, sLine); 
    return BSTR(tmp[n]);  <- you return a local variable
    }
    If you return a local volatile variable to the calling function, the memory the variable lived cannot be guaranteed to there.
    Volatile means it goes away when the function returns.

    Try:
    Use a Global variable

    Pass a pointer to the return value into the funtion

    Use a static pointer in the function and malloc (one time only)
    memory. You have to clean up on exit with atexit()

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Basingstoke
    Posts
    86

    Exclamation

    My code looks like :

    #include <windows.h>

    char *rtn;

    BOOL APIENTRY DllMain(HANDLE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved)
    {
    return TRUE;
    }

    BSTR WINAPI FormatID(HWND hWnd, LPCSTR sLine)
    {
    //char *tmp = new char[strlen(sLine)];
    int n;

    n =<to be calculated>;
    strcpy(rtn, sLine);
    return BSTR(rtn[n]);
    }

    Sorry jim mcnamara could you show me the rest of what you were saying.
    Cheers.

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    What's wrong with passing in a buffer for the character you want to get back?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You're casting the UNICODE value of the n'th character to a pointer and return this. You have a huge memory leak. I think that's reason enough for failure...
    Code:
    BSTR WINAPI FormatID(HWND hWnd, LPCSTR sLine)
    {
      // the tmp is completly unnecessary
      int n;
    
      n = <to be calculated>;
      LPWSTR str;
      int iNeed = MultiByteToWideChar(CP_ACP, 0, sLine+n, 1, NULL, 0);
      str = new WCHAR[iNeed];
      iNeed = MultiByteToWideChar(CP_ACP, 0, sLine+n, 1, str, iNeed);
      if(iNeed == 0)
      {
        // ERROR!
      }
      BSTR out = SysAllocString(str);
      delete[] str;
      return out;
    }
    This is the only correct way to return a BSTR to VB.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Basingstoke
    Posts
    86
    Many Thanks CornedBee

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width