|
201
|
Programación / Programación Visual Basic / Re: [Ayuda] DLL en VB
|
en: 15 Octubre 2010, 18:27 pm
|
. Como Dato: Private Function MTrim(ByVal s As String) As String Dim i As Long Dim res As String For i = 1 To Len(s) If Mid$(s, i, 1) <> Chr$(0) Then res = res & Mid$(s, i, 1) End If Next MTrim = res End Function
Queda mejor asi!¡: Private Function NullTrim(ByVal vDataIn As String) As String Dim Lng_Pos As Long Lng_Pos = InStr(1, vDataIn, Chr(0)) - 1 If Lng_Pos > 0 Then NullTrim = Mid$(vDataIn, 1, Lng_Pos) End Function
Para quitar los chr(0) de los string ( Unicode) solo se me ocurrre hacer esto en un array de bytes. Dim arrbyte() As Byte arrbyte() = StrConv("hola", VbStrConv.vbFromUnicode) MsgBox Chr(arrbyte(0)) & Chr(arrbyte(1)) & Chr(arrbyte(2)) & Chr(arrbyte(3))
Dulces Lunas!¡. Si la función en VB tiene vbFromUnicode, en C++, se me crashea, eso lo pensé y lo intenté pero sin resultados favorables. Lo has probado?
|
|
|
202
|
Programación / Programación Visual Basic / Re: [Ayuda] DLL en VB
|
en: 14 Octubre 2010, 21:34 pm
|
Hola, finalmente pude lograrlo. Código de la DLL: MIREN QUE HAY COSAS QUE LAS PUSE PARA HACER DEDUCCIONES Y PRUEBAS.Option Explicit Public Const DLL_PROCESS_DETACH = 0 ':El proceso descarga la DLL Public Const DLL_PROCESS_ATTACH = 1 ': Cuando un proceso carga la DLL Public Const DLL_THREAD_ATTACH = 2 ': El proceso está recargando una DLL Public Const DLL_THREAD_DETACH = 3 ':El proceso está descargando una Dll recargada Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long Public Function DllMain(hInst As Long, fdwReason As Long, lpvReserved As Long) As Boolean Select Case fdwReason Case DLL_PROCESS_DETACH 'No per-process cleanup needed Case DLL_PROCESS_ATTACH MsgBox "Project1.dll Injected!" 'MsgBox "End of Search!" DllMain = True Case DLL_THREAD_ATTACH 'No per-thread initialization needed Case DLL_THREAD_DETACH 'No per-thread cleanup needed End Select 'DllMain = True End Function Public Sub Mensaje() MessageBox 0&, "Test", "Mensaje", 0& End Sub Public Function Suma(ByVal N1 As Long, ByVal N2 As Long) As Long Suma = N1 + N2 'MessageBox 0, Suma, "Resultado", 0 End Function 'Public Function Mensaje2(ByVal Msg As String) As String 'Mensaje2 = Msg 'End Function Public Function Mensaje2() As String ' Las cadenas de VB6 son Unicode y al usarla desde una DLL ' se hace un follón... así que debemos quitarles los Chr$(0) ' que tenga en medio 'Dim str As String * 4 'str = "Hola" 'Mensaje2 = str Mensaje2 = "Esto es una prueba." End Function Public Function LenString(ByVal Cadena As String) As Integer LenString = Len(Cadena) End Function Private Function MTrim(ByVal s As String) As String Dim i As Long Dim res As String For i = 1 To Len(s) If Mid$(s, i, 1) <> Chr$(0) Then res = res & Mid$(s, i, 1) End If Next MTrim = res End Function
En C++, lo pueden abrir desde VC++ 6.0 (Mi favorito) o del VS 2009, etc. HAY MUCHO CÓDIGO PORQUE PROBÉ DEMASIADAS COSAS.#include <iostream> #include <windows.h> #include <string> using namespace std; typedef void (WINAPI*MsgFunction) (); typedef long (WINAPI*SumaFunction) (long,long); void MyDLL(); void MyDLL2(); void MyDLL3(); void MyDLL4(); string MTrim(char *); string MTrim(char *, int); int tStrLen(char *); bool IsValid(char); //Declaramos el ProcID de la dll //HINSTANCE hGetProcIDDLL = LoadLibrary("Project1.dll"); int main() { MyDLL4(); /* MsgFunction MsgBox; SumaFunction Suma; HINSTANCE hinstDLL = LoadLibrary("Project1.dll"); if(hinstDLL != 0) { printf("DLL LOADED.\n"); MsgBox = (MsgFunction)GetProcAddress(hinstDLL,"Mensaje"); MsgBox(); Suma = (SumaFunction)GetProcAddress(hinstDLL,"Suma"); long x = Suma(6,6); if(x == 12) { cout << "Message Displayed!\n"; } // Unload DLL file FreeLibrary(hinstDLL); } else { printf("DLL NOT LOADED.\n"); } */ system("PAUSE"); return 0; } void MyDLL() { /* get handle to dll */ HINSTANCE hGetProcIDDLL = LoadLibrary("Project1.dll"); /* get pointer to the function in the dll*/ FARPROC lpfnGetProcessID = GetProcAddress((HMODULE)hGetProcIDDLL,"Mensaje"); /* Define the Function in the DLL for reuse. This is just prototyping the dll's function. A mock of it. Use "stdcall" for maximum compatibility. */ typedef void (__stdcall * pICFUNC)(HWND); pICFUNC MyFunction; if (hGetProcIDDLL==NULL) { printf("Can not open the Library\n"); } else { printf("CAN open the Library\n"); } MyFunction = (pICFUNC)lpfnGetProcessID; /* The actual call to the function contained in the dll */ MyFunction(NULL); /* Release the Dll */ FreeLibrary(hGetProcIDDLL); } void MyDLL2() { /* get handle to dll */ HINSTANCE hGetProcIDDLL = LoadLibrary("Project1.dll"); /* get pointer to the function in the dll*/ FARPROC lpfnGetProcessID = GetProcAddress((HMODULE)hGetProcIDDLL,"Suma"); /* Define the Function in the DLL for reuse. This is just prototyping the dll's function. A mock of it. Use "stdcall" for maximum compatibility. */ typedef long (__stdcall * pICFUNC)(long,long); pICFUNC MyFunction; if (hGetProcIDDLL==NULL) { printf("Can not open the Library\n"); } else { printf("CAN open the Library\n"); } MyFunction = (pICFUNC)lpfnGetProcessID; /* The actual call to the function contained in the dll */ long x = MyFunction(5,5); //printf("Result: %l\n",x); cout <<"Resultado: "; cout << x; cout << '\n'; cout <<"Resultado: " << x <<'\n'; //cout <<"El valor decimal de número es:" << numero << '\n'; //cout <<"El valor octal de número es:" << oct <<numero << '\n'; //cout <<"El valor hexadecimal de número es:" << hex <<numero << '\n'; //Si se hace otro cout de algun numero, lo muestra en hex, xq fue //el último que usamos /* Release the Dll */ FreeLibrary(hGetProcIDDLL); } void MyDLL3() { /* get handle to dll */ HINSTANCE hGetProcIDDLL = LoadLibrary("Project1.dll"); /* get pointer to the function in the dll*/ FARPROC lpfnGetProcessID = GetProcAddress((HMODULE)hGetProcIDDLL,"Mensaje2"); /* Define the Function in the DLL for reuse. This is just prototyping the dll's function. A mock of it. Use "stdcall" for maximum compatibility. */ typedef char * (__stdcall * pICFUNC)(); //LPCSTR //typedef void (CALLBACK* pICFUNC)(); pICFUNC MyFunction; if (hGetProcIDDLL==NULL) { printf("Can not open the Library\n"); } else { printf("CAN open the Library\n"); } MyFunction = (pICFUNC)lpfnGetProcessID; /* The actual call to the function contained in the dll */ //MessageBox(NULL, MyFunction(),"CAPTION",MB_OK); char * ss = MyFunction(); //cout << MTrim(ss) << "\n"; /* Release the Dll */ FreeLibrary(hGetProcIDDLL); } void MyDLL4() { /* get handle to dll */ HINSTANCE hGetProcIDDLL = LoadLibrary("Project1.dll"); /* get pointer to the function in the dll*/ FARPROC lpfnGetProcessID = GetProcAddress((HMODULE)hGetProcIDDLL,"Mensaje2"); FARPROC lpfnGetProcessID2 = GetProcAddress((HMODULE)hGetProcIDDLL,"LenString"); /* Define the Function in the DLL for reuse. This is just prototyping the dll's function. A mock of it. Use "stdcall" for maximum compatibility. */ typedef char * (__stdcall * pICFUNC)(); //LPCSTR typedef int (__stdcall * pICFUNC2)(char *); //LPCSTR //typedef void (CALLBACK* pICFUNC)(); pICFUNC MyFunction; pICFUNC2 MyFunction2; if (hGetProcIDDLL==NULL) { printf("Can not open the Library\n"); } else { printf("CAN open the Library\n"); } MyFunction = (pICFUNC)lpfnGetProcessID; MyFunction2 = (pICFUNC2)lpfnGetProcessID2; /* The actual call to the function contained in the dll */ //MessageBox(NULL, MyFunction(),"CAPTION",MB_OK); char * ss = MyFunction(); cout << "Longitud: " << MyFunction2(ss) << "\n\n"; cout << "Supuesta cadena: " << ss << "\n\n"; cout << "Forma Frutera: " << MTrim(ss) << "\n\n"; cout << "A la gran Misery: " << MTrim(ss, MyFunction2(ss)) << "\n\n"; /* Release the Dll */ FreeLibrary(hGetProcIDDLL); } string MTrim(char * s) { long i; string res=""; char chr = char(0); //cout << "Longitud: " << tStrLen("Hola") << "\n"; //cout << "Longitud: " << strlen(s) << "\n"; //cout << "Longitud: " << sizeof(s) << "\n"; //cout << "Longitud: " << s.length() << "\n"; for(i=0; i < 16 ;i++) //Testin' { if(s[i] != chr) { res = res + s[i]; } } return res; } string MTrim(char * s,int len) { long i; string res=""; char chr = char(0); int acum=0; //cout << "Longitud: " << tStrLen("Hola") << "\n"; //cout << "Longitud: " << strlen(s) << "\n"; //cout << "Longitud: " << sizeof(s) << "\n"; //cout << "Longitud: " << s.length() << "\n"; for(i=0; acum < len ;i++) //Testin' { if(IsValid(s[i]) == true && s[i] != chr) { res = res + s[i]; acum++; } } return res; } bool IsValid(char s) { char valid[] = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMÑOPQRSTUVWXYZ0123456789 ,.-!$%&/()=?¿*;_¬[]{}"; for(int i = 0; i < strlen(valid); i++) { if(valid[i] == s) { return true; } } return false; } //Función que devuelve la longitud de una cadena int tStrLen(char * s) { char * s0= s; while (*s++); return s - s0 - 1; }
Lo último que hice fue basado en el, MyDLL4(). Lo que hago es llamar a la función de que me retorna un STRING, luego con una misma funcion de la DLL obtengo la longitud del string, porque no podía obtener la logitud verdadera. MyFunction es el Mensaje, osea, lo que retorna la CADENA. Con MyFunction2 obtengo la longitud. Después uso un MTrim re frutero que lo hice después de muchos intentos xD. El MTrim con 2 parámetros es el que nos interesa. Que le paso la cadena y la longitud, y ahí está como hice, osea sólo permito ciertos dígitos y luego voy contando cuantos caracteres fueron agregandose para saber si es la longitud de la cadena. char * ss = MyFunction(); cout << "Longitud: " << MyFunction2(ss) << "\n\n"; cout << "Supuesta cadena: " << ss << "\n\n"; cout << "Forma Frutera: " << MTrim(ss) << "\n\n"; cout << "A la gran Misery: " << MTrim(ss, MyFunction2(ss)) << "\n\n";
Bueno, desde ya muchísimas gracias a todos y a BlackZeroX por darme esa pista. . Suerte. PD: Obviamente está todo desprolijo y no optimizado, pero así estuve por más de 3 días. PD2: Me falta hacer el pasaje del MessageBox.
|
|
|
203
|
Programación / Programación Visual Basic / Re: [Ayuda] DLL en VB
|
en: 13 Octubre 2010, 19:56 pm
|
Interesante, pero como paso esto a C++?
Private Function MTrim(ByVal s As String) As String Dim i As Long Dim res As String
For i = 1 To Len(s) If Mid$(s, i, 1) <> Chr$(0) Then res = res & Mid$(s, i, 1) End If Next
MTrim = res End Function
Cuando lo tenga en C++, teoricamente tendría que funcionar? :O:O:O. Desde ya muchas gracias.
|
|
|
206
|
Programación / Programación C/C++ / Re: [Ayuda] C++ llamando Dll
|
en: 12 Octubre 2010, 23:08 pm
|
Gracias, pero eso utiliza referencias, osea reutiliza el codigo de la dll, aca tengo la dll en la carpeta con esa función sola, para testear el programa y un proyecto en VC++ el cuál estoy tratando de llamar a esa funcion. Lh: No hagas doble post, utiliza el botón modificar.Pude lograr que no tire error, pero no me muestra el MsgBox "Mensaje!" #include <iostream> #include <windows.h> using namespace std; typedef void (*MsgFunction) (void); HINSTANCE hinstDLL; int main() { MsgFunction MsgBox; hinstDLL = LoadLibrary("Project1.dll"); if(hinstDLL != 0) { printf("DLL LOADED.\n"); MsgBox = (MsgFunction)GetProcAddress(hinstDLL,"Mensaje"); if(MsgBox != 0) { printf("FUNCTION FOUND.\n"); //Call Function MsgBox; } else { printf("FUNCTION NOT FOUND.\n"); } // Unload DLL file FreeLibrary(hinstDLL); } else { printf("DLL NOT LOADED.\n"); } system("PAUSE"); return 0; }
Lh: Uniendo mensajes nuevamente, la próxima borro sin aviso. Utiliza el botón modificar.Pude arreglar el problema, uno era en typedef, que le tenía que agregar WINAPI y otro en llamar a la función, para eso hice 2 funciones, una Mensaje y otra Suma, la Suma lo hace bien sin errores, pero a la hora de llamar a Mensaje, me muestra el mensaje ("Test") y luego crashea el programa tirando error ModName: msvbm60.dll. Alguna idea? Acá les dejo todo el código: Option Explicit Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long Public Sub Mensaje() MessageBox 0, "Test", "Caption", 0 End Sub Public Function Suma(ByVal n1 As Long, ByVal N2 As Long) As Long Suma = n1 + N2 End Function
#include <iostream> #include <windows.h> using namespace std; typedef void (WINAPI*MsgFunction) (); typedef long (WINAPI*SumaFunction) (long,long); int main() { MsgFunction MsgBox; SumaFunction Suma; HINSTANCE hinstDLL = LoadLibrary("Project1.dll"); if(hinstDLL != 0) { printf("DLL LOADED.\n"); MsgBox = (MsgFunction)GetProcAddress(hinstDLL,"Mensaje"); MsgBox(); Suma = (SumaFunction)GetProcAddress(hinstDLL,"Suma"); long x = Suma(6,6); if(x == 12) { cout << "Message Displayed!\n"; } // Unload DLL file FreeLibrary(hinstDLL); } else { printf("DLL NOT LOADED.\n"); } system("PAUSE"); return 0; }
|
|
|
207
|
Programación / Programación C/C++ / [Ayuda] C++ llamando Dll
|
en: 12 Octubre 2010, 22:28 pm
|
Hola a todos, tengo una duda. Tengo una Dll hecha en VB6, Public Sub Mensaje() Msgbox "Mensaje!" End Sub
Como llamo a esa funcion desde C++?, ya probé con LoadLibrary y me tira error, // DLL function signature typedef double (*importFunction)(void); importFunction MyFunction; // Load DLL file HINSTANCE hinstLib = LoadLibrary("Project1.dll"); if (hinstLib == NULL) { MessageBox(0,"ERROR: unable to load DLL","Error",0); } else { // Get function pointer MyFunction = (importFunction)GetProcAddress(hinstLib, "Mensaje"); if (MyFunction == NULL) { MessageBox(0,"ERROR: unable to find DLL function","Error",0); FreeLibrary(hinstLib); } else { MyFunction(); // Unload DLL file FreeLibrary(hinstLib); } }
alguna idea? Desde ya muchas gracias, si quieren subo los archivos. Edit: Es verdad , lo que pasa es que estaba muy desesperado xDDDDD, ahi lo etiqueté
|
|
|
208
|
Programación / Programación Visual Basic / Re: [Ayuda] Callings Sub y Functions
|
en: 11 Octubre 2010, 23:55 pm
|
Hola, quería saber si me podrían guiar con este tema: Yo inyecto una dll en un programa, el programa tiene muchas funciónes y procedimientos, como hago para llamarlos desde la dll inyectada? alguna idea? Desde ya muchas gracias por resolver y aclarar mis dudas en los posts. Ej: del ejecutable. Option Explicit Private Sub Command1_Click() Dim var As Long var = VarPtr(Valor(1, 1)) MsgBox var & " (" & Hex(var) & ")" End Sub Public Function Valor(ByVal v1 As Long, ByVal v2 As Long) As Long Valor = v1 + v2 End Function Esto hice para saber el address de la función. lo que haces hay no es devolver la dirección del proceso, devuelves la dirrecion de variable de RESULTADO del Procesosi quieres saber la dirrecion del Proceso, Funcion, u otra cosa similar En un Modulo ( Bas) Option Explicit Sub main() Dim ThisAddress& ThisAddress& = Adrs&(AddressOf procesoX) MsgBox ThisAddress& & " - (" & Hex(ThisAddress&) & ")" End Sub Public Function Adrs(ByVal Addrs As Long) As Long Adrs& = Addrs& End Function Public Function procesoX(ParamArray ParametrosX() As Variant) As String End Function
Dulce Infierno Lunar!¡. Y como puedo hacer ahora para llamar esta funcion desde una dll (Injected) , desde ya muchisimas gracias
|
|
|
209
|
Programación / Programación Visual Basic / Re: Como Desbloquear un Array...
|
en: 10 Octubre 2010, 08:02 am
|
Hola, el error 10 es porque ya dimensionaste antes el array, ej: Esto tendrías que poner: Dim arr() As Long ReDim arr(0 To 5) arr(0) = 12 arr(1) = 13 arr(2) = 14 arr(3) = 15 arr(4) = 16 arr(5) = 17
Pero tu codigo sigue funcionando mal . Asi que lo hice a mano, tal vez lo puedas mejorar o arreglar, Private Sub Form_Load() Dim arr() As Long ReDim arr(0 To 5) arr(0) = 12 arr(1) = 13 arr(2) = 14 arr(3) = 15 arr(4) = 16 arr(5) = 17 NewRemoveInArrayLong 4, arr End Sub Private Function NewRemoveInArrayLong(ByVal Index&, ByRef ThisArray() As Long) As Boolean Dim tArray() As Long Dim i As Integer If Not IsArray(ThisArray) Or Index& = -1 Then NewRemoveInArrayLong = False Exit Function End If If Index& = UBound(ThisArray) Then ReDim Preserve ThisArray(LBound(ThisArray) To (UBound(ThisArray) - 1)) NewRemoveInArrayLong = True Exit Function Else ReDim tArray(LBound(ThisArray) To (UBound(ThisArray) - 1)) For i = LBound(ThisArray) To UBound(ThisArray) If i < Index& Then tArray(i) = ThisArray(i) ElseIf i > Index& Then tArray(i - 1) = ThisArray(i) End If Next i ReDim ThisArray(LBound(tArray) To UBound(tArray)) ThisArray = tArray Erase tArray NewRemoveInArrayLong = True Exit Function End If NewRemoveInArrayLong = False End Function
Y muchas gracias por ayudarme en el otro post , igualmente tuve una nueva duda, si puedes héchale un vistazo.
|
|
|
|
|
|
|