-
July 30th, 2009, 02:16 PM
#1
export from dll
Though the title sounds simple, the problem seems to be rather tricky...
So I want to call a function from the dll which I have no any reference for.
In particular
1) the dll is 'msvbvm60.dll', a virtual machine for one widely known programming language
2) the function is 'rtcMsgBox'
There also is a code snippet:
Code:
#include <windows.h>
#include <cassert>
//----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,
int nCmdShow )
{
typedef void (* procedure_type) ( const void *argument_1, const void *argument_2 );
HMODULE module_handle = LoadLibrary( "msvbvm60.dll" );
assert( SUCCEEDED(module_handle) );
procedure_type procedure = (procedure_type)GetProcAddress( module_handle, "rtcMsgBox" );
assert( procedure != NULL );
procedure( (const void *)L"", (const void *)L"" );
FreeLibrary( module_handle );
return 0;
}
While assertions are successful, application fails with MS apologies for inconvenience...
-
July 30th, 2009, 11:27 PM
#2
Re: export from dll
Well the fact is you might not be definite about the specs of the function. The types of the arguments is what am talking about. Are you 100% sure function rtcMsgBox is accepting both arguments of type const void * ?
regards
-
July 31st, 2009, 07:02 AM
#3
Re: export from dll
Yes, I don't know the prototypes. Here is the problem: how to get them?
By the way there is no difference (at low level) between between const void * and say char * or even wchar_t * on x86. They all are 4-byte integers.
-
July 31st, 2009, 04:28 PM
#4
Re: export from dll
Basically when you need to load a DLL in vb the dll must be replacing vb strings with BSTR.
The same way I assumet the function you mentioned may be accepting BSTR.
Apart from this there is a tool called specmaker that wine developers created to dump down the specs of functions in windows shared libraries. You cant find its windows version anywhere on web, but I have ported it perfectly to windows NT and it works for me, the reason I am not posting it here is it may be counted a macilius executable, so I will be putting it alongwith an article that I will post at codeguru for dealing with dlls that you dont know.
regards
-
August 1st, 2009, 06:07 PM
#5
Re: export from dll
It's also possible I have to initialize this library, i.e. to call some kind of function other than DllMain (which by the way is called automatically).
-
August 2nd, 2009, 12:02 PM
#6
Re: export from dll
Well, I wonder why this must be done such a fancy way? VB virtual machine belongs with VB code naturally...
Best regards,
Igor
-
August 3rd, 2009, 09:45 AM
#7
Re: export from dll
1) I do this to understand Windows better, get experience in work with different kind of tools and so on. Also export from dll helps me to understand assembler. I'm not going to write code in assembler, as the process in not effective.
2) At the moment I have the following:
Code:
#include <windows.h>
//#include <wtypes.h>
#include <oleauto.h>
#include <stdlib.h>
#include <wctype.h>
#include <stdint.h>
#include <assert.h>
//----------------------------------------------------------------------------
char *allocate_bstr( const wchar_t *string )
{
int32_t length = wcslen( string );
char *result = (char *)malloc( 4 + length * 2 + 2 );
memcpy( result , &length , 4 );
memcpy( result + 4 , string , length * 2 );
memcpy( result + 4 + length * 2 , 0x0000 , 2 );
return result;
}
//----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,
int nCmdShow )
{
typedef void (* procedure_type) ( BSTR arg_1 );
BSTR string = //(BSTR)allocate_bstr( L"I am a happy BSTR" );
SysAllocString( (const OLECHAR *)L"I am a happy BSTR" );
//NULL;
HMODULE module_handle = LoadLibrary( "msvbvm60.dll" );
assert( SUCCEEDED(module_handle) );
procedure_type procedure = (procedure_type)GetProcAddress( module_handle, "rtcMsgBox" );
assert( procedure != NULL );
procedure( string );
FreeLibrary( module_handle );
//free( string );
return 0;
}
This code compiles with (BSTR)allocate_bstr( L"I am a happy BSTR" ) and program fails with no msg box,
and the following compile time error occurs on MinGW 4.4.0,
Code:
C:\DOCUME~1\andrey\LOCALS~1\Temp\ccI7IhPE.o:loadlib.cpp:(.text+0x95): undefined reference to `SysAllocString@4'
collect2: ld returned 1 exit status
I've also tried procedure type with 2 argument with and without return value of type BSTR.
Last edited by andrey_zh; August 3rd, 2009 at 09:51 AM.
-
August 4th, 2009, 12:44 AM
#8
Re: export from dll
1) Okay, that explains alot, but... I believe, before starting to mess with "different kind of tools" it would be good to get some very basic Windows programming skills and knowlege. Afraid, you try to skip this important phase, though as lots of beginners do.
2) A really happy BSTR pointer must point at string part but not length (see documentation on BSTR). The compile error tells me that you're missing some header file to be included.
Best regards,
Igor
-
August 4th, 2009, 09:07 PM
#9
Re: export from dll
Hi,
I'm coming it at from a low level perspective, but one way is to call the function without arguments and look at the stack pointer on return.
The stack pointer will typically move by the number of bytes expected by the function (if it takes any), but it assumes much, and yuo must be prepared for a few headaches.
Another option is to dig out a debugger, again call without parameters, and examine the function. You might be able to figure out not only how many parameters to pass, but what it does, too.
It's the only way to figure out undocumentd features.
What you basically need to do is disassemble the runtime library, but I think you might be in trouble with MS if you do.
Best regards,
AstroTux.
-
August 6th, 2009, 05:15 AM
#10
Re: export from dll
Disassembling MS library is illegal, though nobody could proof it I'm not going to do this, because cracking is not a good way of getting knowledge.
But watching SP ( or ESP in protected mode, as I remember ) in my own program seems to be a fair use.
Though it's not an assembly programming topic I would like to ask:
What debugger would you recommend?
1) SoftIce seems to be dead...is it still usable?
2) What about WinDbg?
3) Any others?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|