RE: Copy to clipboard from a Win console program? - I'm having a problem!
- Posted by jacques deschĂȘnes <desja at globetrot??r.net> Dec 04, 2007
- 633 views
Sorry to tell you that Greg but the your conclip.ew is not designed as it should be according to MSDN. pointers used for clipboard operation must be allocated using GlobalAlloc(). Although conclip.ew seem to works fine, you can't certified it will in all cases. In November 1997 I written the following code which does essentially the same except it use global memory. ref: http://www.rapideuphoria.com/api_wrap.zip
-- NAME: winclip.ew -- PURPOSE: wrapper for win32 clipboard functions. -- DATE: November 21th, 1997 -- BY: Jacques Deschenes, e-mail: desja at globetrotter.qc.ca -- revision history: -- July 29th, 1998 -- added -- CopyTextToClipboard() and GetTextFromClipboard() -- alloc_text_clip() and peek_text_clip() include machine.e include dll.e include user32.ew -- those constant have been moved to user32.ew --global constant -- WM_COPY = 769, -- WM_CUT = 768, -- WM_PASTE = 770, -- WM_ASKCBFORMATNAME = 780, -- WM_CHANGECBCHAIN = 781, -- WM_DESTROYCLIPBOARD = 775, -- WM_DRAWCLIPBOARD = 776, -- WM_HSCROLLCLIPBOARD = 782, -- WM_PAINTCLIPBOARD = 777, -- WM_RENDERALLFORMATS = 774, -- WM_RENDERFORMAT = 773, -- WM_SIZECLIPBOARD = 779, -- WM_VSCROLLCLIPBOARD = 778 global constant CF_BITMAP = 2, CF_DIB = 8, CF_PALETTE = 9, CF_ENHMETAFILE = 14, CF_METAFILEPICT = 3, CF_OEMTEXT = 7, CF_TEXT = 1, CF_UNICODETEXT = 13, CF_DIF = 5, CF_DSPBITMAP = 130, CF_DSPENHMETAFILE = 142, CF_DSPMETAFILEPICT = 131, CF_DSPTEXT = 129, CF_GDIOBJFIRST = 768, CF_GDIOBJLAST = 1023, CF_HDROP = 15, CF_LOCALE = 16, CF_OWNERDISPLAY = 128, CF_PENDATA = 10, CF_PRIVATEFIRST = 512, CF_PRIVATELAST = 767, CF_RIFF = 11, CF_SYLK = 4, CF_WAVE = 12, CF_TIFF = 6 atom iChangeClipboardChain, iCloseClipboard, iCountClipboardFormats, iEmptyClipboard, iEnumClipboardFormats, iGetClipboardData, iGetClipboardFormatName, iGetClipboardOwner, iGetClipboardViewer, iGetOpenClipboardWindow, iGetPriorityClipboardFormat, iIsClipboardFormatAvailable, iOpenClipboard, iRegisterClipboardFormat, iSetClipboardData, iSetClipboardViewer, iGlobalAlloc, iGlobalFree, iDeleteMetaFile, iGlobalLock, iGlobalUnlock, iGlobalSize global procedure ChangeClipboardChain(atom hwnd, atom hNextWnd) c_proc(iChangeClipboardChain,{hwnd, hNextWnd}) end procedure global procedure CloseClipboard() c_proc(iCloseClipboard,{}) end procedure global function CountClipboardFormats() return c_func(iCountClipboardFormats,{}) end function global function EmptyClipboard() return c_func(iEmptyClipboard,{}) end function global function EnumClipboardFormats(atom format) return c_func(iEnumClipboardFormats,{format}) end function global function GetClipboardData(atom format) return c_func(iGetClipboardData,{format}) end function global function GetClipboardFormatName(atom format) atom pStr, count sequence name pStr = allocate(33) count = c_func(iGetClipboardFormatName,{format, pStr,32}) if count then name = peek({pStr,count}) free(pStr) return name else free(pStr) return 0 end if end function global function GetClipboardOwner() return c_func(iGetClipboardOwner,{}) end function global function GetClipboardViewer() return c_func(iGetClipboardViewer,{}) end function global function GetOpenClipboardWindow() return c_func(iGetOpenClipboardWindow,{}) end function global function GetPriorityClipboardFormat(sequence list) atom pList, result pList = allocate(4*length(list)) poke4(pList,list) result = c_func(iGetPriorityClipboardFormat,{pList,length(list)}) free(pList) return result end function global function IsClipboardFormatAvailable(atom format) return c_func(iIsClipboardFormatAvailable,{format}) end function global function OpenClipboard(atom hwnd) return c_func(iOpenClipboard,{hwnd}) end function global function RegisterClipboardFormat(sequence formatName) atom pStr, ok pStr = allocate_string(formatName) ok = c_func(iRegisterClipboardFormat,{pStr}) free(pStr) return ok end function global function SetClipboardData(atom format, atom hData) return c_func(iSetClipboardData,{format,hData}) end function global function SetClipboardViewer(atom hwnd) return c_func(iSetClipboardViewer,{hwnd}) end function -- global memory allocation stuff -- global memory is shared by all processes (programs) global constant -- allocation flags constants GMEM_FIXED = 0, GMEM_MOVEABLE = 2, GMEM_MODIFY = 128, GPTR = 64, GHND = 66, GMEM_DDESHARE = 8192, GMEM_DISCARDABLE = 256, GMEM_LOWER = 4096, GMEM_NOCOMPACT = 16, GMEM_NODISCARD = 32, GMEM_NOT_BANKED = 4096, GMEM_NOTIFY = 16384, GMEM_SHARE = 8192, GMEM_ZEROINIT = 64, GMEM_DISCARDED = 16384, GMEM_INVALID_HANDLE = 32768, GMEM_LOCKCOUNT = 255, GMEM_CLIPBOARD = or_all({GMEM_MOVEABLE,GMEM_DDESHARE}) global function GlobalAlloc(atom flags, integer nBytes) return c_func(iGlobalAlloc,{flags,nBytes}) end function global procedure GlobalFree(atom hmem) atom ok ok = c_func(iGlobalFree,{hmem}) end procedure global function GlobalLock(atom hGlobalMem) return c_func(iGlobalLock,{hGlobalMem}) end function global procedure GlobalUnlock(atom GlobalPtr) c_proc(iGlobalUnlock,{GlobalPtr}) end procedure global function GlobalSize(atom hGlobal) return c_func(iGlobalSize,{hGlobal}) end function global procedure DeleteMetaFile(atom hmeta) atom ok ok = c_func(iDeleteMetaFile,{hmeta}) end procedure -------------- helper functions and procedures global function alloc_text_clip(sequence string) -- allocate string in global memory atom hGlobal, pData string &= 0 hGlobal = GlobalAlloc(GMEM_CLIPBOARD,length(string)) if not hGlobal then return NULL end if pData = GlobalLock(hGlobal) if not pData then GlobalFree(hGlobal) return NULL end if poke(pData,string) GlobalUnlock(hGlobal) return hGlobal end function global function peek_text_clip(atom hCF_DATA) -- peek a string from clipboard object atom pData sequence clip integer size, zero pData = GlobalLock(hCF_DATA) if not pData then return {} end if size = GlobalSize(hCF_DATA) clip = peek({pData,size}) zero = find(0,clip) GlobalUnlock(hCF_DATA) return clip[1..zero-1] end function global function GetTextFromClipboard(atom hOwner) -- get CF_TEXT data from clipboard -- return -1 if coudn't get clipboard ownership atom ok, hClip sequence clip if not OpenClipboard(hOwner) then return -1 end if clip = {} if IsClipboardFormatAvailable(CF_TEXT) then hClip = GetClipboardData(CF_TEXT) clip = peek_text_clip(hClip) end if CloseClipboard() return clip end function global function CopyTextToClipboard(atom hOwner, sequence text) -- copy CF_TEXT data to clipboard -- return 1 if succeed else return 0 atom ok, hClip if not OpenClipboard(hOwner) then return 0 end if if not EmptyClipboard() then return 0 end if hClip = alloc_text_clip(text) if not hClip then return 0 end if ok = SetClipboardData(CF_TEXT,hClip) CloseClipboard() return ok end function ----------------------------------- procedure InitWinClip() atom user32, kernel32, gdi32 user32 = open_dll("user32.dll") kernel32 = open_dll("kernel32.dll") gdi32 = open_dll("gdi32.dll") if find(0,{user32,kernel32,gdi32}) then puts(1,"Failed to open dll library.\n") abort(1) end if iChangeClipboardChain = define_c_proc(user32,"ChangeClipboardChain", {C_UINT,C_UINT}) iCloseClipboard = define_c_proc(user32,"CloseClipboard",{}) iCountClipboardFormats = define_c_func(user32,"CountClipboardFormats", {},C_INT) iEmptyClipboard = define_c_func(user32,"EmptyClipboard",{},C_INT) iEnumClipboardFormats = define_c_func(user32,"EnumClipboardFormats", {C_UINT},C_UINT) iGetClipboardData = define_c_func(user32,"GetClipboardData",{C_UINT},C_UINT) iGetClipboardFormatName = define_c_func(user32,"GetClipboardFormatNameA", {C_UINT,C_UINT,C_UINT},C_UINT) iGetClipboardOwner = define_c_func(user32,"GetClipboardOwner",{},C_UINT) iGetClipboardViewer = define_c_func(user32,"GetClipboardViewer",{},C_UINT) iGetOpenClipboardWindow = define_c_func(user32,"GetOpenClipboardWindow", {},C_UINT) iGetPriorityClipboardFormat = define_c_func(user32, "GetPriorityClipboardFormat",{C_UINT,C_UINT},C_UINT) iIsClipboardFormatAvailable = define_c_func(user32, "IsClipboardFormatAvailable",{C_UINT},C_INT) iOpenClipboard = define_c_func(user32,"OpenClipboard",{C_UINT},C_INT) iRegisterClipboardFormat = define_c_func(user32, "RegisterClipboardFormatA",{C_UINT},C_UINT) iSetClipboardData = define_c_func(user32,"SetClipboardData", {C_UINT,C_UINT},C_UINT) iSetClipboardViewer = define_c_func(user32,"SetClipboardViewer", {C_UINT},C_UINT) iGlobalAlloc = define_c_func(kernel32,"GlobalAlloc",{C_INT,C_INT},C_INT) iGlobalFree = define_c_func(kernel32,"GlobalFree",{C_INT},C_INT) iDeleteMetaFile = define_c_func(gdi32,"DeleteMetaFile",{C_INT},C_INT) iGlobalLock = define_c_func(kernel32,"GlobalLock",{C_INT},C_INT) iGlobalUnlock = define_c_proc(kernel32,"GlobalUnlock",{C_INT}) iGlobalSize = define_c_func(kernel32,"GlobalSize",{C_INT},C_INT) if find(-1,{ iChangeClipboardChain, iCloseClipboard, iCountClipboardFormats, iEmptyClipboard, iEnumClipboardFormats, iGetClipboardData, iGetClipboardFormatName, iGetClipboardOwner, iGetClipboardViewer, iGetOpenClipboardWindow, iGetPriorityClipboardFormat, iIsClipboardFormatAvailable, iOpenClipboard, iRegisterClipboardFormat, iSetClipboardData, iSetClipboardViewer, iGlobalAlloc, iGlobalFree, iDeleteMetaFile, iGlobalLock, iGlobalUnlock, iGlobalSize }) then puts(1,"Winclip.ew initialisation failed.\n" & "One of the dll function was not found.\n") end if end procedure InitWinClip()
regards, Jacqques DeschĂȘnes