The progress may be too fast, and the previous chapter is not very well grasped. When I was reading Chapter 10th, I felt that many knowledge points were not clear, and the structure of this chapter was not clear before, it can be said that it is a bit confusing. Review can help you better understand the previous content. Here, the key point is to thoroughly repeat the textbook code and understand how to write the code.
The code in this chapter is also an old problem. The author of this book does not spend time explaining code debugging. Due to the long history, according to The nmake-F makefile mentioned in the book, many errors may occur, and it takes some time to adjust and compile and debug the files. Now I will write down some experiences in debugging the code in this chapter.
These old codes are generally compiled using the nmake command. In the new vs environment, we can import them to the vs environment to better track the code and understand the essence of COM. However, the compilation process is not well understood or experienced, and cannot be easily modified.
The title of this chapter is server in exe. One is to assume the server role in the same process, and the other is to assume the server role in different processes.
Go to the original code directory chap10 and find the makefile. If you do not understand the makefile, what is it? Well, you have to learn makefile first.
always:nmake -f make-onenmake -f make-one OUTPROC=1
We can see that these two commands are executed to open the make-one file and find the all Tag:
all : Client.exe $(TARGETS)
Then find the client.exe compilation dependency.
Client.exe : Client.obj Guids.obj Util.objlink $(EXE_LINK_FLAGS) Client.obj Guids.obj Util.obj \libcmtd.lib libcimtd.lib $(LIBS)
Guids. OBJ? This file does not exist in the code. What should I do? Read the book again. This is what IDL is doing! You need to use the midl tool to generate this item! Why? Read books! There is such a line in the make-one file:
midl /h iface.h /iid guids.c /proxy proxy.c server.idl
That's it! Use the server. IDL file to generate a header file and two C files.
Well, here is the content of client.exe. Do you still remember the & (targets? Now let's take a look at how to compile this item:
!IF "$(OUTPROC)" == ""!MESSAGE Building in-proc server.SERVER =TARGETS = Server.dllDIR_SERVER = InProc!ELSE!MESSAGE Building local/remote server.SERVER=/D_OUTPROC_SERVER_TARGETS = Server.exe Proxy.dllDIR_SERVER = OutProc!ENDIF
Recall that the second Compilation instruction in makefile carries the outproc = 1 parameter, and the first is not. In addition, you can find this description: Use outproc = 1 to build out-of-Proc version. based on the content in the book, we can understand that services in the process are carried without the outproc = 1 parameter, and services in the process are carried. Let's start with in-process services, that is, generating server. dll. Find the compilation dependency:
SERVER_OBJS = $(DIR_SERVER)\Server.obj \ $(DIR_SERVER)\Cmpnt1.obj \ $(DIR_SERVER)\Cmpnt2.obj \ $(DIR_SERVER)\Cmpnt3.obj \ $(DIR_SERVER)\Registry.obj \ $(DIR_SERVER)\Cfactory.obj \ $(DIR_SERVER)\Cunknown.obj \ $(DIR_SERVER)\Util.obj \ Guids.obj!IF "$(OUTPROC)" == ""Server.dll: $(SERVER_OBJS) Server.def link $(DLL_LINK_FLAGS) $(SERVER_OBJS) libcmtd.lib \libcimtd.lib $(LIBS) /DEF:Server.def regsvr32 -s Server.dll
It is clear what obj is required, and then to the dependencies of OBJ, you can list all the required file sets.
$(DIR_SERVER)\server.obj : server.cpp cunknown.h cfactory.h iface.hcl $(CPP_FLAGS) /Fo"$*.obj" server.cpp$(DIR_SERVER)\cmpnt1.obj : cmpnt1.cpp cmpnt1.h iface.h registry.h \CUnknown.hcl $(CPP_FLAGS) /Fo"$*.obj" cmpnt1.cpp$(DIR_SERVER)\cmpnt2.obj : cmpnt2.cpp cmpnt2.h iface.h registry.h \CUnknown.hcl $(CPP_FLAGS) /Fo"$*.obj" cmpnt2.cpp$(DIR_SERVER)\cmpnt3.obj : cmpnt3.cpp cmpnt3.h iface.h registry.h \CUnknown.hcl $(CPP_FLAGS) /Fo"$*.obj" cmpnt3.cpp$(DIR_SERVER)\CUnknown.obj : CUnknown.cpp CUnknown.hcl $(CPP_FLAGS) $(SERVER) /Fo"$*.obj" CUnknown.cpp$(DIR_SERVER)\CFactory.obj : CFactory.cpp CFactory.hcl $(CPP_FLAGS) $(SERVER) /Fo"$*.obj" CFactory.cpp$(DIR_SERVER)\registry.obj : registry.cpp registry.hcl $(CPP_FLAGS) $(SERVER) /Fo"$*.obj" registry.cpp# util.cpp compiled for server.$(DIR_SERVER)\util.obj : util.cpp util.hcl $(CPP_FLAGS) $(SERVER) /Fo"$*.obj" util.cpp
Now the basic engineering logic is clear. Two projects are required: client.exe project, server. DLL is a service component project (You must register it before using it. It is a long time to register it ). Iface. h guids. C proxy. C is generated by using an IDL file and must be completed before vs project compilation (of course, you can also add an IDL file to the project to set the name of the generated file ).
I just follow the steps above to build the project and debug it to understand the code. Most of the cases where Char is used are changed to tchar for running. Upload the project I created to the resource and hope to help me learn COM technology like me.
In addition, the project compilation of out-of-process service components is not mentioned above. The key is to start with makefile and understand the compilation steps based on the content in the book (the premise is that the content in the book is correct, of course), and then figure out the resources and files required to build several projects, what should I pay attention to during running.
$(DIR_SERVER)\server.obj : server.cpp cunknown.h cfactory.h iface.hcl $(CPP_FLAGS) /Fo"$*.obj" server.cpp$(DIR_SERVER)\cmpnt1.obj : cmpnt1.cpp cmpnt1.h iface.h registry.h \CUnknown.hcl $(CPP_FLAGS) /Fo"$*.obj" cmpnt1.cpp$(DIR_SERVER)\cmpnt2.obj : cmpnt2.cpp cmpnt2.h iface.h registry.h \CUnknown.hcl $(CPP_FLAGS) /Fo"$*.obj" cmpnt2.cpp$(DIR_SERVER)\cmpnt3.obj : cmpnt3.cpp cmpnt3.h iface.h registry.h \CUnknown.hcl $(CPP_FLAGS) /Fo"$*.obj" cmpnt3.cpp$(DIR_SERVER)\CUnknown.obj : CUnknown.cpp CUnknown.hcl $(CPP_FLAGS) $(SERVER) /Fo"$*.obj" CUnknown.cpp$(DIR_SERVER)\CFactory.obj : CFactory.cpp CFactory.hcl $(CPP_FLAGS) $(SERVER) /Fo"$*.obj" CFactory.cpp$(DIR_SERVER)\registry.obj : registry.cpp registry.hcl $(CPP_FLAGS) $(SERVER) /Fo"$*.obj" registry.cpp# util.cpp compiled for server.$(DIR_SERVER)\util.obj : util.cpp util.hcl $(CPP_FLAGS) $(SERVER) /Fo"$*.obj" util.cpp
Http://download.csdn.net/detail/eagleatustb/4972005