--- mstools/samples/rpc/handles/cxhndl/cxhndlc.c 2018/08/09 18:20:56 1.1.1.1 +++ mstools/samples/rpc/handles/cxhndl/cxhndlc.c 2018/08/09 18:24:17 1.1.1.3 @@ -1,26 +1,27 @@ /**************************************************************************** - Microsoft RPC Version 1.0 - Copyright Microsoft Corp. 1992 - cxhndl Example - - FILE: cxhndlc.c - USAGE: cxhndlc -n network_address - -p protocol_sequence - -e endpoint - -o options - -u uuid - -f filename - - PURPOSE: Client side of RPC distributed application - FUNCTIONS: main() - binds to server and calls remote procedure - COMMENTS: + Microsoft RPC Version 1.0 + Copyright Microsoft Corp. 1992 + cxhndl Example + + FILE: cxhndlc.c + + USAGE: cxhndlc -n network_address + -p protocol_sequence + -e endpoint + -o options + -f filename + + PURPOSE: Client side of RPC distributed application + + FUNCTIONS: main() - binds to server and calls remote procedure + + COMMENTS: This distributed application uses a context handle. ****************************************************************************/ -#include -#include + #include -#include // RPC API functions, types -#include "cxhndl.h" // header file generated by MIDL compiler +#include +#include "cxhndl.h" // header file generated by MIDL compiler #define PURPOSE \ @@ -29,7 +30,6 @@ the use of the [context_handle] attribut about attributes and RPC API functions, see the RPC programming\n\ guide and reference.\n\n" -#define BUFSIZE 1024 void Usage(char * pszProgramName) { @@ -39,65 +39,61 @@ void Usage(char * pszProgramName) fprintf(stderr, " -n network_address\n"); fprintf(stderr, " -e endpoint\n"); fprintf(stderr, " -o options\n"); - fprintf(stderr, " -u uuid\n"); fprintf(stderr, " -f filename\n"); exit(1); } -void main(int argc, char **argv) +void _CRTAPI1 main(int argc, char **argv) { - RPC_STATUS status; // returned by RPC API function + RPC_STATUS status; PCONTEXT_HANDLE_TYPE phContext = NULL; - char * pbBuf = NULL; - short cbRead; /* count of bytes read */ + unsigned char * pbBuf = NULL; + short cbRead; // count of bytes read - unsigned char * pszUuid = "12345678-1234-1234-1234-123456789ABC"; + unsigned char * pszUuid = NULL; unsigned char * pszProtocolSequence = "ncacn_np"; unsigned char * pszNetworkAddress = NULL; - unsigned char * pszEndpoint = "\\pipe\\context"; + unsigned char * pszEndpoint = "\\pipe\\cxhndl"; unsigned char * pszOptions = NULL; - unsigned char * pszStringBinding = NULL; - unsigned char * pszFileName = "readme.cxh"; + unsigned char * pszStringBinding = NULL; + unsigned char * pszFileName = "readme.txt"; int i; - // allow the user to override settings with command line switches + /* allow the user to override settings with command line switches */ for (i = 1; i < argc; i++) { - if ((*argv[i] == '-') || (*argv[i] == '/')) { - switch (tolower(*(argv[i]+1))) { - case 'p': // protocol sequence - pszProtocolSequence = argv[++i]; - break; - case 'n': // network address - pszNetworkAddress = argv[++i]; - break; - case 'e': - pszEndpoint = argv[++i]; - break; - case 'o': - pszOptions = argv[++i]; - break; - case 'u': - pszUuid = argv[++i]; - break; - case 'f': - pszFileName = argv[++i]; - break; - case 'h': - case '?': - default: - Usage(argv[0]); - } - } - else - Usage(argv[0]); + if ((*argv[i] == '-') || (*argv[i] == '/')) { + switch (tolower(*(argv[i]+1))) { + case 'p': // protocol sequence + pszProtocolSequence = argv[++i]; + break; + case 'n': // network address + pszNetworkAddress = argv[++i]; + break; + case 'e': + pszEndpoint = argv[++i]; + break; + case 'o': + pszOptions = argv[++i]; + break; + case 'f': + pszFileName = argv[++i]; + break; + case 'h': + case '?': + default: + Usage(argv[0]); + } + } + else + Usage(argv[0]); } - pbBuf = (char *) malloc(BUFSIZE * sizeof(char)); - -/* Use a convenience function to concatenate the elements of */ -/* the string binding into the proper sequence. */ + pbBuf = (unsigned char *) + midl_user_allocate(BUFSIZE * sizeof(unsigned char)); + /* Use a convenience function to concatenate the elements of */ + /* the string binding into the proper sequence. */ status = RpcStringBindingCompose(pszUuid, pszProtocolSequence, pszNetworkAddress, @@ -106,62 +102,67 @@ void main(int argc, char **argv) &pszStringBinding); printf("RpcStringBindingCompose returned 0x%x\n", status); printf("pszStringBinding = %s\n", pszStringBinding); - if (status) - exit(2); - -/* Set the binding handle that will be used to bind to the server. */ + if (status) { + exit(status); + } + /* Set the binding handle that will be used to bind to the server. */ status = RpcBindingFromStringBinding(pszStringBinding, - &hStarter); + &hStarter); printf("RpcBindingFromStringBinding returned 0x%x\n", status); - if (status) - exit(2); + if (status) { + exit(status); + } printf("Calling the remote procedure RemoteOpen\n"); if (RemoteOpen(&phContext, pszFileName) < 0) { - printf("Unable to open %s\n", pszFileName); - Shutdown(); - exit(2); + printf("Unable to open %s\n", pszFileName); + Shutdown(); + exit(2); } - /* now the context handle also manages the binding */ - + /* Now the context handle also manages the binding. */ status = RpcBindingFree(&hStarter); printf("RpcBindingFree returned 0x%x\n", status); - if (status) - exit(2); + if (status) { + exit(status); + } printf("Calling the remote procedure RemoteRead\n"); - while (RemoteRead(&phContext, pbBuf, &cbRead) > 0) { - for (i = 0; i < cbRead; i++) - putchar(*(pbBuf+i)); + while (RemoteRead(phContext, pbBuf, &cbRead) > 0) { + for (i = 0; i < cbRead; i++) + putchar(*(pbBuf+i)); } printf("Calling the remote procedure RemoteClose\n"); if (RemoteClose(&phContext) < 0 ) { - printf("Close failed on %s\n", pszFileName); - exit(2); + printf("Close failed on %s\n", pszFileName); + exit(2); } -/* The calls to the remote procedures are complete. */ -/* Free the string and the binding handle */ - - - status = RpcStringFree(&pszStringBinding); // remote calls done; unbind + /* The calls to the remote procedures are complete. */ + /* Free the string and the binding handle */ + status = RpcStringFree(&pszStringBinding); printf("RpcStringFree returned 0x%x\n", status); - if (status) - exit(2); + if (status) { + exit(status); + } exit(0); -} +} // end main() + + +/*********************************************************************/ +/* MIDL allocate and free */ +/*********************************************************************/ -void * MIDL_user_allocate(size_t len) +void __RPC_FAR * __RPC_API midl_user_allocate(size_t len) { return(malloc(len)); } -void MIDL_user_free(void * ptr) +void __RPC_API midl_user_free(void __RPC_FAR * ptr) { free(ptr); }