problema escribiendo archivo vb6 api(caracteres raros)

Iniciado por z_ane_666, 8 Septiembre 2011, 14:17 PM

0 Miembros y 1 Visitante están viendo este tema.

z_ane_666

buenos dias ,he implementado un code para escribir contenido en un archivo (que ha de estar creado con el nombre ajhgk.txt y en la c:\ antes de usar el programa), realmente funciona, pero escribe caracteres extraños

Option Explicit
Const MOVEFILE_REPLACE_EXISTING = &H1
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const FILE_BEGIN = 0
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const CREATE_ALWAYS As Long = 2
Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20
Const GENERAL_WRITE = 1073741824



Private Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hfile _
As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh _
As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hfile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hfile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" ( _
                ByVal hfile As Long, _
                lpFileSizeHigh As Long) As Long
Private Sub Command1_Click()

  Dim sSave As String, hOrgFile As Long, hNewFile As Long, bBytes() As Byte
    Dim sTemp As String, nSize As Long, Ret As Long
   
Dim charbuffer As String
charbuffer = "Anonymous!"
hNewFile = CreateFile("c:\ajhgk.txt", GENERIC_WRITE, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
WriteFile hNewFile, charbuffer, 10, Ret, ByVal 0&

CloseHandle hNewFile
End Sub


en vez de escribir Anonymous! escribe ¼NÇT:#   


espero que me ayuden

saludos

$Edu$


z_ane_666

felizmente hace poco halle la solución, era pasar por byval el buffer, aca el codigo
Option Explicit
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_ALWAYS = 4
Private Const FILE_BEGIN = 0


Private Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hfile _
As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh _
As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hfile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hfile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" ( _
               ByVal hfile As Long, _
               lpFileSizeHigh As Long) As Long
Private Sub Command1_Click()

 Dim sSave As String, hOrgFile As Long, hNewFile As Long, bBytes() As Byte
   Dim sTemp As String, nSize As Long, Ret As Long
   
Dim charbuffer As String
charbuffer = "Anonymous!"
hNewFile = CreateFile("c:\ajhgk.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_ALWAYS, 0, 0)
WriteFile hNewFile, ByVal charbuffer, 10, Ret, ByVal 0&

CloseHandle hNewFile
End Sub



Disculpen por desviar el tema, pero creo que es mas comodo usar las funciones de visual basic . lo de arriba lo postee ya que las funciones de vb no escriben archivos de manera secuencial, sino linea por linea (caso append) y decici usar el api, pero veo que es complicadito...

¿alguna idea de poder hacer que la salida de append, la haga concatenada y no por linea a linea?

saludos

raul338

No recuerdo exactamente cual era el metodo que lo hacia sin poner salto de linea, Print lo hace, creo que era Put o Write  :-\

x64core

bueno yo mas o menos tenia el mismo problemas hace un tiempo :P
pues lo que puedes hace es eso, concatenar las cadenas :P
creo que tendras que obtener primero el valor de la cadena y luego unirla con la nueva cadena :P para que el resultado quede asi:

ABCDEFG

y no asi:

A
B
C
D
E
F

pero aun asi, si lo haces asi es vergonzoso :P :xD