CALLC.C
资源名称:MSDN_VC98.zip [点击查看]
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:5k
源码类别:
Windows编程
开发平台:
Visual C++
- /****************************************************************************
- Microsoft RPC Version 2.0
- Copyright Microsoft Corp. 1992, 1993, 1994- 1996
- Callback Example
- FILE: callc.c
- USAGE: callc -n network_address
- -p protocol_sequence
- -e endpoint
- -o options
- -v value to compute fibonacci number
- PURPOSE: Client side of RPC distributed application
- FUNCTIONS: main() - binds to server and calls remote procedure
- COMMENTS: This sample program generates a Fibonacci number by
- static callback.
- ****************************************************************************/
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include "call.h" // header file generated by MIDL compiler
- void Usage(char * pszProgramName)
- {
- fprintf(stderr, "Usage: %sn", pszProgramName);
- fprintf(stderr, " -p protocol_sequencen");
- fprintf(stderr, " -n network_addressn");
- fprintf(stderr, " -e endpointn");
- fprintf(stderr, " -o optionsn");
- fprintf(stderr, " -v value_for_Fibonaccin");
- exit(1);
- }
- void _CRTAPI1 main(int argc, char **argv)
- {
- RPC_STATUS status;
- unsigned char * pszUuid = NULL;
- unsigned char * pszProtocolSequence = "ncacn_np";
- unsigned char * pszNetworkAddress = NULL;
- unsigned char * pszEndpoint = "\pipe\callback";
- unsigned char * pszOptions = NULL;
- unsigned char * pszStringBinding = NULL;
- short sValue = 2;
- short sFibNumber;
- int i;
- /* 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 'v':
- sValue = atoi(argv[++i]);
- break;
- case 'h':
- case '?':
- default:
- Usage(argv[0]);
- }
- }
- else
- Usage(argv[0]);
- }
- /* Use a convenience function to concatenate the elements of */
- /* the string binding into the proper sequence. */
- status = RpcStringBindingCompose(pszUuid,
- pszProtocolSequence,
- pszNetworkAddress,
- pszEndpoint,
- pszOptions,
- &pszStringBinding);
- printf("RpcStringBindingCompose returned 0x%xn", status);
- printf("pszStringBinding = %sn", pszStringBinding);
- if (status) {
- exit(status);
- }
- /* Set the binding handle that will be used to bind to the server. */
- status = RpcBindingFromStringBinding(pszStringBinding,
- &callback_IfHandle);
- printf("RpcBindingFromStringBinding returned 0x%xn", status);
- if (status) {
- exit(status);
- }
- printf("Calling 'Fibonacci(n)' for n = %dn", sValue);
- RpcTryExcept {
- sFibNumber = Fibonacci(sValue); // make call
- printf("The Fibonacci number of %d = %dn", sValue, sFibNumber);
- printf("Calling the remote procedure 'Shutdown'n");
- Shutdown(); // shut down the server side
- }
- RpcExcept(1) {
- printf("Runtime reported exception %ldn", RpcExceptionCode() );
- }
- RpcEndExcept
- /* The calls to the remote procedures are complete. */
- /* Free the string and the binding handle */
- status = RpcStringFree(&pszStringBinding); // remote calls done; unbind
- printf("RpcStringFree returned 0x%xn", status);
- if (status) {
- exit(status);
- }
- status = RpcBindingFree(&callback_IfHandle); // remote calls done; unbind
- printf("RpcBindingFree returned 0x%xn", status);
- if (status) {
- exit(status);
- }
- exit(0);
- } // end main()
- /* callback function */
- short Fibonacci2(short n)
- {
- short nsub1, nsub2;
- printf("Callback: Fibonacci2 called with n = %dn", n);
- if ((n == 0) || (n == 1))
- return(1);
- else {
- nsub1 = n - 1;
- nsub2 = n - 2;
- return(Fibonacci(nsub1) + Fibonacci(nsub2));
- }
- }
- /*********************************************************************/
- /* MIDL allocate and free */
- /*********************************************************************/
- 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);
- }
- /* end file callc.c */