problem with RPC code

i got the source code from this website:
http://www.aspfree.com/c/a/net/introduction-to-rpc-on-windows-part-i/

and when i tried running the server and client i get Runtime exception occured:5
in the client console which means access denied

i don't know how to fix it
heres the client code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include "..\RPC1_IDL\DoRPC.h"

int main()
{
   RPC_STATUS status;
   unsigned char* szStringBinding = NULL;

   // Creates a string binding handle.
   // This function formats the passed values in a 
   // predefined format for use by RPC. Just like printf
   // Connection is not done here.
   status = RpcStringBindingCompose(
      NULL, // UUID to bind to.
      (unsigned char*)("ncacn_ip_tcp"), // Use TCP/IP protocol.
      (unsigned char*)("localhost"), // TCP/IP network
      (unsigned char*)("1234"), // TCP/IP port to use.
      NULL, 				// Protocol dependent network options to use.
      &szStringBinding); 		// String binding output.

   if (status)
      exit(status);

   // Validates the format of the string binding handle and converts
   // it to a binding handle.
   // Connection is not done here either.
   status = RpcBindingFromStringBinding(
      szStringBinding, // The string binding to validate.
      &hDoRPCBinding); // Put the result in the implicit binding
                          // handle defined in the IDL file.

	if(status)
	{
		exit(status);
	}

	RpcTryExcept
	{
		// Calls the RPC function. The hDoRPCBinding binding handle
		// is used implicitly.
		// Connection is done here.
		const unsigned char szMsg[] = "Client: I Can RPC Now!";
		Show(szMsg);
	}
	RpcExcept(1)
	{
		printf("Runtime exception occured: %d\n",RpcExceptionCode());
	}
	RpcEndExcept

	// Free the memory allocated by a string.
	status = RpcStringFree(&szStringBinding); // String to be freed.

	if(status)
	{
		exit(status);
	}

	// Releases binding handle resources and disconnects from the server.
	status = RpcBindingFree(
	&hDoRPCBinding);	// Frees the implicit binding handle defined in
						// the IDL file.

	if (status)
	{
		exit(status);
	}

	system ("PAUSE");

	return 0;
}

// Memory allocation function for RPC.
// The runtime uses these two functions for allocating/deallocating
// enough memory to pass the string to the server.
void* __RPC_USER midl_user_allocate(size_t size)
{
	return malloc(size);
}

// Memory deallocation function for RPC.
void __RPC_USER midl_user_free(void* p)
{
	free(p);
}

Last edited on
i debugged the program and found that the problem was with the line in the show function
NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );

here is the show function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  void Show( 
    /* [string][in] */ const unsigned char __RPC_FAR *szMsg)
{

    RPC_BINDING_HANDLE _Handle  =   0;

    RPC_MESSAGE _RpcMessage;

    MIDL_STUB_MESSAGE _StubMsg;

    if(!szMsg)
        {
        RpcRaiseException(RPC_X_NULL_REF_POINTER);
        }
    RpcTryFinally
        {
        NdrClientInitializeNew(
                          ( PRPC_MESSAGE  )&_RpcMessage,
                          ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                          ( PMIDL_STUB_DESC  )&DoRPC_StubDesc,
                          0);


        _Handle = hDoRPCBinding;


        _StubMsg.BufferLength = 12U;
        NdrConformantStringBufferSize( (PMIDL_STUB_MESSAGE) &_StubMsg,
                                       (unsigned char __RPC_FAR *)szMsg,
                                       (PFORMAT_STRING) &__MIDL_TypeFormatString.Format[4] );

        NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );

        NdrConformantStringMarshall( (PMIDL_STUB_MESSAGE)& _StubMsg,
                                     (unsigned char __RPC_FAR *)szMsg,
                                     (PFORMAT_STRING) &__MIDL_TypeFormatString.Format[4] );

        NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );

        }
    RpcFinally
        {
        NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );

        }
    RpcEndFinally

}


so when debugging it jumped to the RpcExcept line when it reached NdrSendReceive
Not that i understand a bit of that code, but, well, did you allocate enough memory for _StubMsg.Buffer before you call NdrSendReceive()?
Last edited on
the file that the show function is from was automatically generated when i complied a idl file. i haven't changed any of the code from the website i linked in the first post. can you download the code and test it yourself?
Last edited on
well, I can't even compile it. The compiler complains about a switch where I can't find information about.

Honestly, even if it works I wouldn't recommend it. It's way overcomplicated and error prone.

You can easily do it yourself: Just send the name of the function and parameter and on the other side the server will call that function.
This cannot be done by hand. RPC is done this way only. We create stubs and compile them into a C code, which generates server and client code. Then add your own code and lots of things I don't remember now..

I think this is more suitable to post in windows forum.


@coder777 i don't know to make my own RPC program so that's why i tried the tutorial. i also tried this website: http://www.codeproject.com/Articles/4878/Introduction-to-RPC-Part-2
but again i just got errors
Last edited on
Topic archived. No new replies allowed.