home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / mswindo / programm / misc / 1780 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  2.9 KB

  1. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!ira.uka.de!chx400!sicsun!masg1.epfl.ch!gulcu
  2. From: gulcu@masg1.epfl.ch (Ceki Gulcu)
  3. Newsgroups: comp.os.ms-windows.programmer.misc
  4. Subject: Re: Qns abt drag/drop & GlobalUnlock
  5. Message-ID: <1992Sep11.143225@masg1.epfl.ch>
  6. Date: 11 Sep 92 12:32:25 GMT
  7. References: <1992Sep11.052535.1107@nuscc.nus.sg>
  8. Sender: news@sicsun.epfl.ch
  9. Organization: Ecole Polytechnique Federale de Lausanne
  10. Lines: 66
  11.  
  12. In article <1992Sep11.052535.1107@nuscc.nus.sg>, twchan%Solomon.Technet.sg (Chan Tur Wei) writes:
  13. |> 
  14. |> Dear netters,
  15. |> 
  16. |> I'm posting the questions below on behalf of a friend who's having 
  17. |> trouble with some Windows programming problem.  Appreciate if someone
  18. |> could help!
  19. |> 
  20. |>     (1) Supporting "Drag" besides "Drop"
  21. |> 
  22.  
  23.     Sorry, I can't help you on this one.
  24.  
  25. |>     (2) Lock and Free Memory problem
  26. |> 
  27. |>     When an application calls GlobalUnlock() and receives zero in
  28. |>     return, it means that the lock count of the memory handle has
  29. |>     reduced to zero.  However, when the application immediately
  30. |>     tries to free it, it receives a GP fault saying that the
  31. |>     application is trying to free a locked memory object.  This
  32. |>     problem can be worked around by calling another GlobalUnlock()
  33. |>     before GlobalFree().  Therefore, it seems that when
  34. |>     GlobalUnlock() returns a zero, it does not necessarily mean that
  35. |>     the lock count is already decremented to zero.  Is this true?
  36. |> 
  37.  
  38. I have massively used GlobalAlloc/GlobalLock/GlobalUnlock functions. Here is
  39. a simplified version of what I usually do:
  40.  
  41.  
  42. void foo(void)
  43. {
  44.    HANDLE testHandle;      /* memory handle */
  45.    LPSTR  stringPointer;   
  46.    char   sampleString[] = "Hello World!";
  47.  
  48.    /* Allocate sufficient space to accomodate the "Hello World" string */
  49.    /* WARNING: The GMEM_MOVEABLE and GMEM_NONDISCARD constants may contain 
  50.       typos as I am writing code from the back of my head. */
  51.    testHandle = GloabalAlloc(GMEM_MOVEABLE | GMEM_NONDISCARD,  
  52.                  (strlen(sampleString) + 1) * sizeof(char));
  53.  
  54.    stringPointer = (LPSTR) GlobalLock(testHandle);
  55.    strcpy(stringPointer, sampleString);
  56.    GlobalUnlock(testHandle);
  57.    
  58. } /* foo */
  59.  
  60. I have encountered no problemes with the preceding memory management scheme. I 
  61. have used to it create multi-MB sized linked lists.
  62.  
  63. I recommend that your friend uses the malloc/free fuctions at the early stage
  64. of the developement cycle in order to make sure that the logic of his code 
  65. is correct. Only then should he switch to global memory allocation functions.
  66.  
  67. Hope that helps,
  68.  
  69. GULCU Ceki
  70. gc@lbdsun6.epfl.ch
  71.  
  72. ps: Allow me mention that OS/2 lets you simply use malloc() and free() to 
  73.     access upto 512 MB of memory. Also, as Windows and PM are very similar
  74.     from the GUI programmers view, maybe your friend should try OS/2.
  75.     Personally, I rather program for OS/2 but my boss does not see it the
  76.     same way... <-- EOW  (End of whining)
  77.     
  78.