Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ATL COM dll which exports symbols

Posted on 2003-11-10
7
Medium Priority
?
504 Views
Last Modified: 2013-12-14
You can create an ATL COM-dll, this dll has a typelibrary to access the internal functions.

You can also create a DLL which exports some symbols.

But now, I want to create an ATL-COM DLL which exports some functions and structs:

//code//
; DPD_Dll.def : Declares the module parameters.

LIBRARY      "Dll.DLL"

EXPORTS
      DllCanUnloadNow     @1 PRIVATE
      DllGetClassObject   @2 PRIVATE
      DllRegisterServer   @3 PRIVATE
      DllUnregisterServer      @4 PRIVATE
\\code\\

That's from an ATL com DLL...

But now I want to export the struct 'route' and the function 'findroute' and the object (class) 'CMaptool'

How can I do this?
0
Comment
Question by:G00fy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 9

Expert Comment

by:_ys_
ID: 9716059
All be it possible, I highly recommend against it.

COM dynamically loads/unloads dlls as it uses them. As such your dll could be unloaded when a client has a handle on your exported struct or class - which would not be good.

If you really must, your struct can be defined purely in IDL, allowing it to be redistributed as part of the type library.
0
 

Author Comment

by:G00fy
ID: 9716095
No, I just want the Dll to be used OR by accessing it via IDL, or via default windows API's...

If they (the people who use it) want to mix them... I don't care... The IDL frontend has all the objects and functions and structs and such. So it's nearly done.

But now I want to include the API-frontend.


Another question came to my head:
when I access the dll via API, how is the load/unloading done? Is it loaded before every api-call, and unloaded after each api-call? Or stays it resident in memory? Or does it get a ReferenceCounter (like with COM-elements) that when it hits 0, it is unloaded?
0
 
LVL 9

Assisted Solution

by:_ys_
_ys_ earned 160 total points
ID: 9716177
>> No, I just want the Dll to be used OR by accessing it via IDL, or via default windows API's...

Just making you aware of the potential issues.

Prefixing your declaration with __declspec(dllexport), exports symbols from a dll.

__declspec(dllexport) void findRoute( );

You may wish to prefix that prefix with ectern "C" as well. This prevents the C++ compiler from mangling to function name.

extern "C" __declspec(dllexport) void findRoute( );

Without this extern "C" client calls to GetProcAddress("findRoute") would fail.
extern "C" alleviates this and allows it to succeed. (The function names could alternatively be added to the def file to alleviate this).

>> when I access the dll via API, how is the load/unloading done? Is it loaded before every api-call, and unloaded after each api-call? Or stays it resident in memory? Or does it get a ReferenceCounter (like with COM-elements) that when it hits 0, it is unloaded?

Unless 'dll delayed loading' is enabled when compiling a project those referenced dlls are loaded when the process itself loads. They remain resident until the process terminates - or until a call to UnloadLibrary is performed.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 49

Accepted Solution

by:
DanRollins earned 240 total points
ID: 9763089
Using 'normal' non-COM styel access to the DLL, one typically does *not* export data.  One typically provides access functions.  However, it is perfectly possible.  Just follow these steps:

1) Use the AppWizard to create a new app:  
   type: Win32 Dynamic Link Library
   name: MyTestDLL
then [OK]

2) Select "A DLL that exports some symbols"   Then click [Finish]

3) Open MyTestDLL.h and add this to the bottom:

//------------------------ define the data structure
typedef struct {
      int   nLen;
      char  szName[30];
      char  szAddr[30];
} MYSTRUCT;

//------------------------ export an instance of that data
extern MyTestDLL_API MYSTRUCT rExportedStruct;

4) Open MyTestDLL.cpp and add this to the bottom:

MyTestDLL_API MYSTRUCT rExportedStruct= {1,"Dan","anytown USA"};

5)  Build the project.

6)  Create another testing project... Win32 Console.  Make it so:

#include "\MyProj\zEETests2\MyTestDLL\MyTestDLL.h"
#pragma comment(lib, "\\MyProj\\zEETests2\\MyTestDLL\\Debug\\MyTestDLL.lib" )
#include <stdio.h>

void main()
{
      MYSTRUCT* pr= &rExportedStruct;

      printf( "name is %s \r\n", pr->szName );
}

7) Before running this, copy the DLL from the ..\MyTestDLL\Debug and put it into the same directory as the EXE.  
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
And that is all there is to it.

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9784108
G00fy,
Does that help you?
-- Dan
0
 

Author Comment

by:G00fy
ID: 9788966
I have not tried it yet... I will update this question as soon as I start with this... In the meantime ;) you might be able to answer the other question I'm going to ask in a minute on this very same site.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10248847
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: _ys_ {http:#9716177} & DanRollins {http:#9763089}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question