Sign in to follow this  
pdform

Problems with C and COM

Recommended Posts

Hi, there... I'm new here, so be patient... I'm working in a project with C and DirectX. Before you say that, I know that Cpp is a really better choice to work with DirectX but I actually prefer C and I'd like to learn how to work with DirectX in that language. My problem is that, tough I can run Direct3D, I simply couldn't access the member functions to set the scene and render. I believe the problem is the way I'm working with COM. Here goes the code I created to test Direct3D bulding a simple blue window (only the funtions that initialize and set DirectX) and the errors I received from VC++6. If you can help me, please, do that...
line 99 here
//----------------------------------------------------------------------------- 
 // Desc: Inicializa o Direct3D 
 //----------------------------------------------------------------------------- 
 HRESULT InitD3D( HWND hWnd ) 
 { 
    // Estrutura usada para criar o dispositivo 
     D3DPRESENT_PARAMETERS d3dpp; 
 
     // Cria o objeto Direct3D 
     if(NULL==(g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))) 
         return E_FAIL; 
     
    //inicializa biblioteca COM 
    hr = CoInitialize(NULL); 
 
    if (FAILED(hr)) 
       return E_FAIL; 
 
    //configura opções de inicialização 
     ZeroMemory( &d3dpp, sizeof(d3dpp) ); 
     d3dpp.Windowed = TRUE; 
     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; 
     d3dpp.EnableAutoDepthStencil = TRUE; 
     d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 
 
     
    //Cria o Device 
    hr = IDirect3D9_CreateDevice(g_pD3D, 0, D3DDEVTYPE_HAL, hWnd, 
                              0, &d3dpp, &g_pd3dDevice); 
 
 
     if( FAILED(hr)) 
         return E_FAIL; 
 
     return S_OK; 
 } 
 
 //----------------------------------------------------------------------------- 
 // Nome: Cleanup() 
 //----------------------------------------------------------------------------- 
 void Cleanup() 
 { 
     if( g_pd3dDevice != NULL ) 
       IDirect3DDevice9_Release(g_pd3dDevice); 
     
     if( g_pD3D != NULL )        
         IDirect3D9_Release(g_pD3D); 
 
    //fecha a biblioteca COM 
    CoUninitialize(); 
 } 
 
 //----------------------------------------------------------------------------- 
 // Nome: Render() 
 //----------------------------------------------------------------------------- 
 void AdjustStates() 
 { 
    //matriz de projeção 
    D3DXMATRIX matProj; 
        
     //Desliga a oclusão automática de faces 
     hr = g_pd3dDevice -> lpVtbl-> SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE ); 
 
     //Desliga a iluminação da cena 
     hr = g_pd3dDevice -> lpVtbl-> SetRenderState( D3DRS_LIGHTING, FALSE ); 
     
    //zbuffer ligado 
     hr = g_pd3dDevice -> lpVtbl-> SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE ); 
 
     //Ajusta a matriz de projeção 
     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.5f, 1.0f, 100.0f ); 
     hr = g_pd3dDevice -> lpVtbl-> SetTransform( D3DTS_PROJECTION, &matProj ); 
 } 
 
 //----------------------------------------------------------------------------- 
 // Nome: Render() 
 //----------------------------------------------------------------------------- 
 void Render() 
 { 
     // Limpa backbuffer pintando-o de azul 
     hr = g_pd3dDevice -> lpVtbl-> Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
                          D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); 
 
     // Inicia a cena 
     if( SUCCEEDED( hr = g_pd3dDevice -> lpVtbl-> BeginScene() ) ) 
     { 
         // Encerra a cena 
         hr = g_pd3dDevice -> lpVtbl-> EndScene(); 
     } 
 
     // Apresenta o conteúdo do backbuffer (mostra o que foi desenhado) 
     hr = g_pd3dDevice -> lpVtbl-> Present( NULL, NULL, NULL, NULL ); 
 } 
\main.c(162) : warning C4047: 'function' : 'struct IDirect3DDevice9 *' differs in levels of indirection from 'int ' \main.c(162) : warning C4024: 'function through pointer' : different types for formal and actual parameter 1 \main.c(162) : error C2198: 'function through pointer' : too few actual parameters \main.c(165) : warning C4047: 'function' : 'struct IDirect3DDevice9 *' differs in levels of indirection from 'int ' \main.c(165) : warning C4024: 'function through pointer' : different types for formal and actual parameter 1 \main.c(165) : error C2198: 'function through pointer' : too few actual parameters \main.c(168) : warning C4047: 'function' : 'struct IDirect3DDevice9 *' differs in levels of indirection from 'int ' \main.c(168) : warning C4024: 'function through pointer' : different types for formal and actual parameter 1 \main.c(168) : error C2198: 'function through pointer' : too few actual parameters \main.c(172) : warning C4047: 'function' : 'struct IDirect3DDevice9 *' differs in levels of indirection from 'int ' \main.c(172) : warning C4024: 'function through pointer' : different types for formal and actual parameter 1 \main.c(172) : warning C4047: 'function' : 'int ' differs in levels of indirection from 'struct _D3DMATRIX *' \main.c(172) : warning C4024: 'function through pointer' : different types for formal and actual parameter 2 \main.c(172) : error C2198: 'function through pointer' : too few actual parameters \main.c(181) : warning C4047: 'function' : 'unsigned long ' differs in levels of indirection from 'void *' \main.c(181) : warning C4024: 'function through pointer' : different types for formal and actual parameter 2 \main.c(181) : warning C4047: 'function' : 'const struct _D3DRECT *' differs in levels of indirection from 'const long ' \main.c(181) : warning C4024: 'function through pointer' : different types for formal and actual parameter 3 \main.c(182) : warning C4244: 'function' : conversion from 'const float ' to 'unsigned long ', possible loss of data \main.c(182) : error C2198: 'function through pointer' : too few actual parameters \main.c(185) : error C2198: 'function through pointer' : too few actual parameters \main.c(188) : error C2198: 'function through pointer' : too few actual parameters \main.c(192) : error C2198: 'function through pointer' : too few actual parameters

Share this post


Link to post
Share on other sites
Correct me if I am wrong but I thought using the COM interfaces in C, you have to pass the pointer to the object you are calling a function of in AGAIN as the first parameter to the function, to simulate the this pointer implicitly there in C++.

g_pd3dDevice->lpVtbl->BeginScene(g_pd3dDevice);

If this is correct, it would explain why all your errors are "Not enough parameters."

Not too sure about this though.

Share this post


Link to post
Share on other sites
Thank you, guys.

But the fact is that I had never worked explicitaly with COM before this project so I had no idea of that. I'm gonna test. Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this