win32-tests: add a very usefull w32 programm to explore midl features
authorStefan Metzmacher <metze@samba.org>
Wed, 6 Feb 2008 15:34:36 +0000 (16:34 +0100)
committerStefan Metzmacher <metze@samba.org>
Wed, 6 Feb 2008 15:34:36 +0000 (16:34 +0100)
You have to define idl, call the client function
and add a server implementation.

Then you need to compile midltests.exe
and run it, it will dump the generated NDR on the
screen.

metze
(This used to be commit dc15c8833599a1cb8f51c2b5390925410cbf4e12)

testprogs/win32/midltests/midltests.acf [new file with mode: 0644]
testprogs/win32/midltests/midltests.c [new file with mode: 0644]
testprogs/win32/midltests/midltests.idl [new file with mode: 0644]
testprogs/win32/midltests/midltests_c_m.c [new file with mode: 0644]
testprogs/win32/midltests/midltests_marshall.c [new file with mode: 0644]
testprogs/win32/midltests/midltests_marshall.h [new file with mode: 0644]
testprogs/win32/midltests/midltests_s_m.c [new file with mode: 0644]
testprogs/win32/midltests/utils.c [new file with mode: 0644]

diff --git a/testprogs/win32/midltests/midltests.acf b/testprogs/win32/midltests/midltests.acf
new file mode 100644 (file)
index 0000000..a04bb5d
--- /dev/null
@@ -0,0 +1,6 @@
+[
+        implicit_handle (handle_t midltests_IfHandle)
+]
+interface midltests
+{
+}
diff --git a/testprogs/win32/midltests/midltests.c b/testprogs/win32/midltests/midltests.c
new file mode 100644 (file)
index 0000000..61d4ce0
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+   MIDLTESTS client.
+
+   Copyright (C) Stefan Metzmacher 2008
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include "midltests.h"
+
+#define MIDLTESTS_C_CODE 1
+#include "midltests.idl"
+
+int main(int argc, char **argv)
+{
+       int ret;
+
+       midltests_IfHandle = NULL;
+
+       RpcTryExcept {
+               midltests();
+       } RpcExcept(1) {
+               ret = RpcExceptionCode();
+               printf("Runtime error 0x%x\n", ret);
+       } RpcEndExcept
+
+       return ret;
+}
diff --git a/testprogs/win32/midltests/midltests.idl b/testprogs/win32/midltests/midltests.idl
new file mode 100644 (file)
index 0000000..1932549
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef MIDLTESTS_C_CODE
+
+[
+  uuid("225b9fcb-eb3d-497b-8b0b-591f049a2507"),
+  pointer_default(unique)
+]
+interface midltests
+{
+       long midltests_fn(
+       );
+}
+
+#elif MIDLTESTS_C_CODE
+
+static void midltests(void)
+{
+       cli_midltests_fn();
+}
+
+long srv_midltests_fn(void)
+{
+       printf("srv_midltests_fn: Start\n");
+       printf("srv_midltests_fn: End\n");
+       return 0x65757254;
+}
+
+#endif
\ No newline at end of file
diff --git a/testprogs/win32/midltests/midltests_c_m.c b/testprogs/win32/midltests/midltests_c_m.c
new file mode 100644 (file)
index 0000000..facf735
--- /dev/null
@@ -0,0 +1,2 @@
+#include "midltests_marshall.h"
+#include "midltests_c.c"
diff --git a/testprogs/win32/midltests/midltests_marshall.c b/testprogs/win32/midltests/midltests_marshall.c
new file mode 100644 (file)
index 0000000..e772afd
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+   MIDLTESTS client.
+
+   Copyright (C) Stefan Metzmacher 2008
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include "midltests.h"
+
+#define MIN(a,b) ((a)<(b)?(a):(b))
+static void print_asc(const unsigned char *buf,int len)
+{
+        int i;
+        for (i=0;i<len;i++)
+                printf("%c", isprint(buf[i])?buf[i]:'.');
+}
+
+static void dump_data(const unsigned char *buf1,int len)
+{
+        const unsigned char *buf = (const unsigned char *)buf1;
+        int i=0;
+        if (len<=0) return;
+
+        printf("[%03X] ",i);
+        for (i=0;i<len;) {
+                printf("%02X ",(int)buf[i]);
+                i++;
+                if (i%8 == 0) printf(" ");
+                if (i%16 == 0) {
+                        print_asc(&buf[i-16],8); printf(" ");
+                        print_asc(&buf[i-8],8); printf("\n");
+                        if (i<len) printf("[%03X] ",i);
+                }
+        }
+        if (i%16) {
+                int n;
+                n = 16 - (i%16);
+                printf(" ");
+                if (n>8) printf(" ");
+                while (n--) printf("   ");
+                n = MIN(8,i%16);
+                print_asc(&buf[i-(i%16)],n); printf( " " );
+                n = (i%16) - n;
+                if (n>0) print_asc(&buf[i-n],n);
+                printf("\n");
+        }
+}
+
+void NdrGetBufferMarshall(PMIDL_STUB_MESSAGE stubmsg, unsigned long len, RPC_BINDING_HANDLE hnd)
+{
+       stubmsg->RpcMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, len);
+       memset(stubmsg->RpcMsg->Buffer, 0xef, len);
+       stubmsg->RpcMsg->BufferLength = len;
+       stubmsg->Buffer = stubmsg->RpcMsg->Buffer;
+       stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
+       stubmsg->fBufferValid = TRUE;
+}
+
+void __RPC_STUB midltests_midltests_fn(PRPC_MESSAGE _pRpcMessage);
+
+void NdrSendReceiveMarshall(PMIDL_STUB_MESSAGE StubMsg, unsigned char *buffer)
+{
+       unsigned long DataRepresentation;
+
+       StubMsg->RpcMsg->BufferLength = buffer - (unsigned char *)StubMsg->RpcMsg->Buffer;
+
+       printf("[in] Buffer[%d/%d]\n",
+               StubMsg->RpcMsg->BufferLength, StubMsg->BufferLength);
+       dump_data(StubMsg->RpcMsg->Buffer, StubMsg->RpcMsg->BufferLength);
+
+       DataRepresentation = StubMsg->RpcMsg->DataRepresentation;
+       StubMsg->RpcMsg->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
+       midltests_midltests_fn(StubMsg->RpcMsg);
+       StubMsg->RpcMsg->DataRepresentation = DataRepresentation;
+
+       StubMsg->BufferLength = StubMsg->RpcMsg->BufferLength;
+       StubMsg->BufferStart = StubMsg->RpcMsg->Buffer;
+       StubMsg->BufferEnd = StubMsg->BufferStart + StubMsg->BufferLength;
+       StubMsg->Buffer = StubMsg->BufferStart;
+
+       printf("[out] Buffer[%d]\n",
+               StubMsg->RpcMsg->BufferLength);
+       dump_data(StubMsg->RpcMsg->Buffer, StubMsg->RpcMsg->BufferLength);
+}
+
+void NdrServerInitializeNewMarshall(PRPC_MESSAGE pRpcMsg,
+                                   PMIDL_STUB_MESSAGE pStubMsg,
+                                   PMIDL_STUB_DESC pStubDesc)
+{
+       memset(pStubMsg, 0, sizeof(*pStubMsg));
+       pStubMsg->RpcMsg = pRpcMsg;
+       pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
+       pStubMsg->BufferEnd = pStubMsg->Buffer + pRpcMsg->BufferLength;
+       pStubMsg->BufferLength = pRpcMsg->BufferLength;
+       pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
+       pStubMsg->pfnFree = pStubDesc->pfnFree;
+       pStubMsg->StubDesc = pStubDesc;
+       pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
+}
+
+RPC_STATUS WINAPI I_RpcGetBufferMarshall(PRPC_MESSAGE RpcMsg)
+{
+       RpcMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, RpcMsg->BufferLength);
+       memset(RpcMsg->Buffer, 0xcd, RpcMsg->BufferLength);
+       return 0;
+}
diff --git a/testprogs/win32/midltests/midltests_marshall.h b/testprogs/win32/midltests/midltests_marshall.h
new file mode 100644 (file)
index 0000000..0c6aed2
--- /dev/null
@@ -0,0 +1,16 @@
+#include "rpc.h"
+#include "rpcndr.h"
+#define NdrSendReceive NdrSendReceiveMarshall
+void NdrSendReceiveMarshall(PMIDL_STUB_MESSAGE stubmsg, unsigned char *buffer);
+#define NdrGetBuffer NdrGetBufferMarshall
+void NdrGetBufferMarshall(PMIDL_STUB_MESSAGE stubmsg, unsigned long len, RPC_BINDING_HANDLE hnd);
+#define NdrServerInitializeNew NdrServerInitializeNewMarshall
+void NdrServerInitializeNewMarshall(PRPC_MESSAGE pRpcMsg,
+                                   PMIDL_STUB_MESSAGE pStubMsg,
+                                   PMIDL_STUB_DESC pStubDesc);
+#define I_RpcGetBuffer I_RpcGetBufferMarshall
+RPC_STATUS WINAPI I_RpcGetBufferMarshall(PRPC_MESSAGE pMsg);
+
+
+
+
diff --git a/testprogs/win32/midltests/midltests_s_m.c b/testprogs/win32/midltests/midltests_s_m.c
new file mode 100644 (file)
index 0000000..7e0862d
--- /dev/null
@@ -0,0 +1,2 @@
+#include "midltests_marshall.h"
+#include "midltests_s.c"
diff --git a/testprogs/win32/midltests/utils.c b/testprogs/win32/midltests/utils.c
new file mode 100644 (file)
index 0000000..8c4833d
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+   MIDLTESTS utility functions.
+
+   Copyright (C) Tim Potter 2003
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "midltests.h"
+
+/* MIDL allocate and free functions */
+
+void __RPC_FAR *__RPC_USER midl_user_allocate(size_t len)
+{
+               return(malloc(len));
+}
+
+void __RPC_USER midl_user_free(void __RPC_FAR *ptr)
+{
+       free(ptr);
+}