Solved

ATL COM dll which exports symbols

Posted on 2003-11-10
7
494 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
  • 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 40 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 49

Accepted Solution

by:
DanRollins earned 60 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now