Announcement

Collapse
No announcement yet.

Calling PB/DLL from C

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gregery D Engle
    replied
    I'm writing a packet sniffer and it seems that all examples are
    in C so, I'm modifying the examples to perform what I want, (already
    done) and then I'm simply going to call a PB/DLL for every packet
    that is received. I think it will be much easier.

    In that case don't I want BYREF instead of BYVAL?

    Actually I think the function will be more like this:

    function PacketSniff(byref hexdata as dword) as long

    But I think I can modify the C typedef you provided me.

    Thanks

    ------------------
    -Greg

    [This message has been edited by Gregery D Engle (edited January 16, 2001).]

    Leave a comment:


  • Peter P Stephensen
    replied
    This should do it:
    Code:
    #include <windows.h>
     
    typedef int (_stdcall *LPYOGA)(char *,char *,char *,char *); 
     
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    	HINSTANCE hDll;
    	LPYOGA Yoga;
     
    	if((hDll = LoadLibrary("YourDll.dll")) == NULL) 
    	{
    		MessageBox(0, "Could not load YourDll.dll","Test", MB_OK);
    		return 0;
    	}
     
    	if((Yoga = (LPYOGA)GetProcAddress(hDll, "Yoga")) == NULL)
    	{	
    		FreeLibrary(hDll);
    		MessageBox(0, "Could not load function Yoga","Test", MB_OK);
    		return 0;	
    	}
     
    	//Use function
    	int i = Yoga("This", "Is", "a", "test");
     	
    	FreeLibrary(hDll);
    	return 0;
    }
    Regards
    Peter

    ------------------

    Leave a comment:


  • Tom Hanlin
    replied
    A typedef of that nature would be for calling a function loaded using
    LoadLibrary... is that what you have in mind? I'm pretty rusty at C
    calls designed in that fashion, so I'm unlikely to get this quite right,
    but perhaps I can help a bit.

    In C, a "char" is a signed byte type. An ampersand ("&") works much
    like the PowerBASIC function VARPTR, although the ampersand evaluates
    like a pseudo-op rather than a function. It seems probable that there
    are serious things wrong with your C declaration, although it's not
    fully clear what you intend without seeing the PB function header.
    If you intend a C string, you need to declare a char pointer, or "char *"
    in C, and use an ASCIIZ string in PowerBASIC.

    Assuming that your PowerBASIC function looks something like this:
    Code:
    FUNCTION yoga ALIAS "yoga" (strMessage AS ASCIIZ, strAddressOut AS ASCIIZ, _
        strAddress2Out AS ASCIIZ, strSubject AS ASCIIZ) EXPORT AS LONG
    Your C declaration should look more like this:
    Code:
    typedef int __stdcall yoga (char * strMessage, char * strAddressOut, \
        char * strAddress2Out, char * strSubject);
    Seems to me that you need a few extra parentheses in the C code and,
    perhaps, a little bit of redesign for the typedef. I'll see if I can dig
    up the particulars tomorrow, if no one else has been able to clarify all
    this by then.

    ------------------
    Tom Hanlin
    PowerBASIC Staff

    Leave a comment:


  • Gregery D Engle
    started a topic Calling PB/DLL from C

    Calling PB/DLL from C

    I'm having some problems with the prototype in C

    This is what I have so far:
    Code:
    typedef int __stdcall yoga( char &strMessage, 
                                char strAddressOut, 
                                char strAddress2Out, 
                                char strSubject );
    and this is what I get during build:

    d:\packet\wpdpack\examples\testapp\pbdll.h(1) : error C2143: syntax error : missing ')' before '&'
    d:\packet\wpdpack\examples\testapp\pbdll.h(1) : error C2143: syntax error : missing '{' before '&'
    d:\packet\wpdpack\examples\testapp\pbdll.h(1) : error C2059: syntax error : '&'
    d:\packet\wpdpack\examples\testapp\pbdll.h(1) : error C2059: syntax error : ')'

    ------------------
    -Greg
Working...
X