elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Extraño error, puedo imprimir el texto pero no copiarlo.
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Extraño error, puedo imprimir el texto pero no copiarlo.  (Leído 1,900 veces)
kworld

Desconectado Desconectado

Mensajes: 30


Ver Perfil
Extraño error, puedo imprimir el texto pero no copiarlo.
« en: 29 Mayo 2010, 10:39 am »

Quisiera que por favor alguien me ayudara con este código, logró obtener el texto que se necesita, pero no logro copiarlo a otro buffer.

Código
  1. #include <windows.h>
  2.  
  3. UCHAR* getUserSid();
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.   UCHAR* userSid;
  8.   userSid = getUserSid();
  9.   printf("%s",userSid);
  10.  
  11.   return 0;
  12.  
  13. }
  14.  
  15. UCHAR* getUserSid()
  16. {
  17.  HANDLE token = NULL;
  18.  DWORD dwBufferSize = 0;
  19.  PTOKEN_USER pTokenUser = NULL;
  20.  HANDLE currentProcess = NULL;
  21.  UCHAR* userSid = NULL;
  22.  
  23.  currentProcess = GetCurrentProcess();
  24.  if (OpenProcessToken(currentProcess, TOKEN_QUERY, &token))
  25.  {
  26.     GetTokenInformation(token, TokenUser, NULL, 0, &dwBufferSize);
  27.     pTokenUser = (PTOKEN_USER)malloc(dwBufferSize);
  28.     memset(pTokenUser, 0, dwBufferSize);
  29.     if (GetTokenInformation(token, TokenUser, pTokenUser, dwBufferSize, &dwBufferSize))
  30.     {
  31.         if (IsValidSid(pTokenUser->User.Sid))
  32.         {
  33.             UCHAR* localSID = NULL;
  34.             size_t len;
  35.  
  36.             if (ConvertSidToStringSidA(pTokenUser->User.Sid, &localSID))
  37.             {
  38.                printf("%s\n",localSID); //esto se imprime bien
  39.  
  40.                len = strlen(localSID);
  41.                printf("Len:%d\n",len); //esto se imprime bien
  42.  
  43.                userSid = (UCHAR*) malloc(len+1, sizeof(UCHAR));
  44.                if (userSid != NULL)
  45.                {
  46.                  printf("memoria dinamica bien\n"); //esto se imprime bien
  47.                  strncpy(userSid, localSID, len);
  48.                  userSid[len] = 0;
  49.                }
  50.                printf("%s\n",userSid); //esto NO SE IMPRIME BIEN
  51.                LocalFree(localSID);
  52.             }
  53.         }                        
  54.     }
  55.     free(pTokenUser);
  56.     CloseHandle(token);
  57.  }
  58.  CloseHandle(currentProcess);
  59.  return userSid;
  60. }
  61.  
  62.  


En línea

nicolas_cof


Desconectado Desconectado

Mensajes: 348


Into the Wild


Ver Perfil WWW
Re: Extraño error, puedo imprimir el texto pero no copiarlo.
« Respuesta #1 en: 29 Mayo 2010, 11:24 am »

Mmmmm porque estas usando malloc como si fuera un calloc

Código:
userSid = (UCHAR*) malloc(len+1, sizeof(UCHAR));

tendria que quedar asi...

Código:
userSid = (UCHAR*) malloc((len+1) * sizeof(UCHAR));

No te dio ningun error al compilar?

Otra pregunta windows.h sirve para las funciones printf(), malloc() y free(), porque de no ser asi te estarias olvidando de poner las librerias..
Código:
#include <stdio.h>
#include <stdlib.h>

Salu10.


« Última modificación: 29 Mayo 2010, 11:28 am por nicolas_cof » En línea

kworld

Desconectado Desconectado

Mensajes: 30


Ver Perfil
Re: Extraño error, puedo imprimir el texto pero no copiarlo.
« Respuesta #2 en: 29 Mayo 2010, 18:31 pm »

gracias. Haciendo pruebas lo solucioné así:

Código
  1. #include <windows.h>
  2.  
  3. UCHAR* getUserSid();
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.   UCHAR* userSid;
  8.   userSid = getUserSid();
  9.   if (userSid != NULL)
  10.   {
  11.     printf("%s",userSid);
  12.     free(userSid);
  13.   }
  14.   return 0;
  15. }
  16.  
  17. UCHAR* getUserSid()
  18. {
  19.  HANDLE token = NULL;
  20.  DWORD dwBufferSize = 0;
  21.  PTOKEN_USER pTokenUser = NULL;
  22.  HANDLE currentProcess = NULL;
  23.  UCHAR* userSid = NULL;
  24.  
  25.  currentProcess = GetCurrentProcess();
  26.  if (OpenProcessToken(currentProcess, TOKEN_QUERY, &token)) {
  27.     GetTokenInformation(token, TokenUser, NULL, 0, &dwBufferSize);
  28.     pTokenUser = (PTOKEN_USER)malloc(dwBufferSize);
  29.     memset(pTokenUser, 0, dwBufferSize);
  30.     if (GetTokenInformation(token, TokenUser, pTokenUser, dwBufferSize, &dwBufferSize)) {
  31.        if (IsValidSid(pTokenUser->User.Sid)) {
  32.           BOOL WINAPI ConvertSidToStringSidA(PSID, UCHAR*);
  33.           ConvertSidToStringSidA(pTokenUser->User.Sid, (UCHAR*)&userSid);
  34.        }
  35.     }
  36.     free(pTokenUser);
  37.     CloseHandle(token);
  38.  }
  39.  CloseHandle(currentProcess);
  40.  return userSid;
  41. }
  42.  
  43.  
« Última modificación: 29 Mayo 2010, 18:34 pm por kworld » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Problema extraño al insertar texto.
.NET (C#, VB.NET, ASP)
Edu 1 1,960 Último mensaje 24 Abril 2011, 00:29 am
por Edu
error al imprimir
ASM
Flamer 8 4,631 Último mensaje 1 Mayo 2012, 02:01 am
por x64core
Ayuda: Hallar texto en japonés, o copiarlo/traducirlo de imagenes.
Foro Libre
Tachikomaia 3 2,261 Último mensaje 12 Agosto 2018, 06:07 am
por Tachikomaia
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines