IMA read at address 0

Anfängerfragen zum Programmieren mit PureBasic.
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Beitrag von ts-soft »

DLL ist zumindest unter Vista nicht verwendbar, würde ich demnach auch
nicht tun, es sei denn, das Programm soll nur bei Dir laufen.

Korrekte Bufferlängen liefert die API-Funktion ja, man muß es nur
entsprechend nutzen.

Code: Alles auswählen

EnableExplicit

Define FF, size, *mem, result, *unistring
Define text.s

FF = ReadFile(#PB_Any, "test.data")
If FF
  size = Lof(FF)
  *mem = AllocateMemory(size)
  If *mem
    ReadData(FF, *mem, size)
    result = MultiByteToWideChar_(54936, 0, *mem, size, *unistring, 0)
    If result
      *unistring = AllocateMemory(result + 2)
      If *unistring
        If MultiByteToWideChar_(54936, 0, *mem, size, *unistring, result)
          text = PeekS(*unistring, result, #PB_Unicode)
        EndIf
      EndIf
    EndIf
  Else
    CloseFile(FF)
  EndIf
EndIf

OpenWindow(0, #PB_Ignore, #PB_Ignore, 850, 70, "", #PB_Window_SystemMenu)
StringGadget(0, 10, 10, 830, 40, "")
SetGadgetFont(0, FontID(LoadFont(#PB_Any, "SimSun", 24)))
SetGadgetText(0, text)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
Benutzeravatar
D@nte
Beiträge: 324
Registriert: 24.04.2007 15:33
Wohnort: Berlin

Beitrag von D@nte »

nice one ts :D

Das man irgendwie mit der API auch die Länge rausbekommen soll kann war mir bekannt, allerdings durch meine verkappte aktion mit dem

Code: Alles auswählen

output.s = Space(Len(input))
@output
hab ich bei

Code: Alles auswählen

Debug MultiByteToWideChar_(CodePage, dwFlags, @input, StringByteLength(input), @output, 0)
immer 0 bekommen *shy*

Ferner hab ich festgestellt das es, wenn man die Konvertierung mehrfach hintereinander ausführen will

Code: Alles auswählen

result = MultiByteToWideChar_(54936, 0, *mem, size, 0, 0) 
heißen muss weil sonst result beim 2ten Durchlauf 0 wird und bleibt

Code: Alles auswählen

EnableExplicit

Define.l FF, size, *mem, result, *unistring, result2, *utfstring
Define.s text, text2

*unistring = AllocateMemory(1000)
*utfstring = AllocateMemory(1000)

FF = ReadFile(#PB_Any, "X:\test.data")
If FF
  size = Lof(FF)
  *mem = AllocateMemory(size)
  If *mem
    ReadData(FF, *mem, size)
    result = MultiByteToWideChar_(54936, 0, *mem, size, 0, 0)
    Debug result
    If result
      *unistring = ReAllocateMemory(*unistring, result + 2)
      If *unistring
        If MultiByteToWideChar_(54936, 0, *mem, size, *unistring, result)
          text = PeekS(*unistring, result, #PB_Unicode)
        EndIf
      EndIf
    EndIf
    result2 = WideCharToMultiByte_(54936, 0, *unistring, result, 0, 0, 0, 0)
    Debug result2
    If result2
      *utfstring = ReAllocateMemory(*utfstring, result2 + 2)
      If *utfstring
        If WideCharToMultiByte_(54936, 0, *unistring, result, *utfstring, result2, 0, 0)
          text2 = PeekS(*utfstring, result2, #PB_Unicode)
        EndIf
      EndIf
    EndIf
  Else
    CloseFile(FF)
  EndIf
EndIf

OpenWindow(0, #PB_Ignore, #PB_Ignore, 850, 120, "", #PB_Window_SystemMenu)
If CreateGadgetList(WindowID(0))
  StringGadget(0, 10, 10, 830, 40, "")
  StringGadget(1, 10, 60, 830, 40, "")
  SetGadgetFont(0, FontID(LoadFont(#PB_Any, "SimSun", 24)))
  SetGadgetText(0, text)
  SetGadgetFont(1, FontID(LoadFont(#PB_Any, "SimSun", 24)))
  SetGadgetText(1, text2)
EndIf

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Also wenn ich den Speicher vorher alloziiere funktioniert das ganze allerdings nicht wenn ich versuche das ganze zur Laufzeit zu machen, was ich aber eigtl unbedingt können muss, sprich wenn ich statt

Code: Alles auswählen

*utfstring = AllocateMemory(1000)
*utfstring = ReAllocateMemory(*utfstring, result2 + 2)
nur

Code: Alles auswählen

*utfstring = AllocateMemory(result2 + 2)
verwende bekomm ich wieder nen IMA, das ist doch nicht normal oder?!
Oder bin ich einfach nicht in der Lage zu verstehen wie man die Memory-Funktion von PB richtig verwendet?! T_T
Antworten