WinAPIFunction.st
author Claus Gittinger <cg@exept.de>
Thu, 09 Jun 2016 13:02:17 +0200
changeset 3909 10e26efa4fb4
parent 3908 6b71bf0b6d16
child 3912 bc757e3447e6
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1998 by eXept Software AG
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"


ExternalFunction subclass:#WinAPIFunction
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'OS-Windows'
!

!WinAPIFunction primitiveDefinitions!
%{
#ifdef __x86_64__
 // finally, Microsoft got rid of this garbage
 typedef int  (*WINAPI_intFUNC)();
#else
 typedef WINAPI int  (*WINAPI_intFUNC)();
#endif
%}

! !

!WinAPIFunction class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1998 by eXept Software AG
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

!

documentation
"
    WIN32 only:

    Experimental class representing external (non-Smalltalk) WINAPI functions.
    These will provide the same functionality and protocol as
    regular externalFunctions, but use a different calling convention.

    Warning: under construction.

    [author:]
	Claus Gittinger

    [see also:]
	ExternalAddress ExternalBytes
"


! !

!WinAPIFunction methodsFor:'function calling'!

call
    "call the underlying C function, passing no argument.
     The return value is interpreted as an integer
     (and must be converted to an externalBytes object,
      if it is a pointer to something).

     DANGER alert: This is an unprotected low-level entry.
     Not for normal application usage.
    "
%{
    WINAPI_intFUNC func;
    INT retVal;

    func = (WINAPI_intFUNC) __INST(code_);
    if (func) {
	retVal = (*func)();
	RETURN (__MKINT(retVal));
    }
%}.
    self primitiveFailed

    "Created: / 18.6.1998 / 18:01:28 / cg"
    "Modified: / 18.6.1998 / 18:04:48 / cg"
!

callWith:arg
    "call the underlying C function, passing a single argument.
     The argument arg is converted to a corresponding C data type,
     as defined in the convertST_to_C() function.
     The return value is interpreted as an integer
     (and must be converted to an externalBytes object,
      if it is a pointer to something).

     DANGER alert: This is an unprotected low-level entry.
     Not for normal application usage.
    "
%{
    INT convertST_to_C();
    WINAPI_intFUNC func;
    INT retVal;

    func = (WINAPI_intFUNC) __INST(code_);
    if (func) {
	retVal = (*func)(convertST_to_C(arg));
	RETURN (__MKINT(retVal));
    }
%}.
    self primitiveFailed

    "Created: / 18.6.1998 / 18:04:30 / cg"
    "Modified: / 18.6.1998 / 18:04:53 / cg"
!

callWith:arg1 with:arg2
    "call the underlying C function, passing two args.
     The arguments are converted to a corresponding C data type,
     as defined in the convertST_to_C() function.
     The return value is interpreted as an integer
     (and must be converted to an externalBytes object,
      if it is a pointer to something).

     DANGER alert: This is an unprotected low-level entry.
     Not for normal application usage.
    "
%{
    INT convertST_to_C();
    WINAPI_intFUNC func;
    INT retVal;

    func = (WINAPI_intFUNC) __INST(code_);
    if (func) {
	retVal = (*func)(convertST_to_C(arg1), convertST_to_C(arg2));
	RETURN (__MKINT(retVal));
    }
%}.
    self primitiveFailed

    "Created: / 18.6.1998 / 18:05:07 / cg"
!

callWith:arg1 with:arg2 with:arg3
    "call the underlying C function, passing three args.
     The arguments are converted to a corresponding C data type,
     as defined in the convertST_to_C() function.
     The return value is interpreted as an integer
     (and must be converted to an externalBytes object,
      if it is a pointer to something).

     DANGER alert: This is an unprotected low-level entry.
     Not for normal application usage.
    "
%{
    INT convertST_to_C();
    WINAPI_intFUNC func;
    INT retVal;

    func = (WINAPI_intFUNC) __INST(code_);
    if (func) {
	retVal = (*func)(convertST_to_C(arg1), convertST_to_C(arg2), convertST_to_C(arg3));
	RETURN (__MKINT(retVal));
    }
%}.
    self primitiveFailed

    "Created: / 18.6.1998 / 18:05:18 / cg"
!

callWithArguments:argArray
    "call the underlying C function, passing up to 10 arguments.
     The arguments are converted to a corresponding C data type,
     as defined in the convertST_to_C() function.
     The return value is interpreted as an integer
     (and must be converted to an externalBytes object,
      if it is a pointer to something).

     DANGER alert: This is an unprotected low-level entry.
     Not for normal application usage.
    "
%{
    INT convertST_to_C();
    WINAPI_intFUNC func;
#   define NUMARGS 10
    INT args[NUMARGS];
    INT retVal;
    OBJ *ap;

    func = (WINAPI_intFUNC) __INST(code_);
    if (func && __isArrayLike(argArray)) {
	int n = __arraySize(argArray);
	int i;

	if (n <= 10) {
	    ap = __ArrayInstPtr(argArray)->a_element;
	    for (i=0; i<NUMARGS; i++) {
		args[i] = convertST_to_C(*ap++);
	    }
	}
	switch (n) {
	    case 0:
		retVal = (*func)();
		break;
	    case 1:
		retVal = (*func)(args[0]);
		break;
	    case 2:
		retVal = (*func)(args[0], args[1]);
		break;
	    case 3:
		retVal = (*func)(args[0], args[1], args[2]);
		break;
	    case 4:
		retVal = (*func)(args[0], args[1], args[2], args[3]);
		break;
	    case 5:
		retVal = (*func)(args[0], args[1], args[2], args[3],
				 args[4]);
		break;
	    case 6:
		retVal = (*func)(args[0], args[1], args[2], args[3],
				 args[4], args[5]);
		break;
	    case 7:
		retVal = (*func)(args[0], args[1], args[2], args[3],
				 args[4], args[5], args[6]);
		break;
	    case 8:
		retVal = (*func)(args[0], args[1], args[2], args[3],
				 args[4], args[5], args[6], args[7]);
		break;
	    case 9:
		retVal = (*func)(args[0], args[1], args[2], args[3],
				 args[4], args[5], args[6], args[7],
				 args[8]);
		break;
	    case 10:
		retVal = (*func)(args[0], args[1], args[2], args[3],
				 args[4], args[5], args[6], args[7],
				 args[8], args[9]);
		break;
	    default:
		goto err;
	}
	RETURN (__MKINT(retVal));
    }
  err: ;
%}.
    self primitiveFailed

    "Created: / 18.6.1998 / 18:05:46 / cg"
! !

!WinAPIFunction class methodsFor:'documentation'!

version
    ^ '$Header$'
! !