Simple Managed DLL Creation

  • Thread starter Thread starter Mark Prenter
  • Start date Start date
M

Mark Prenter

Hi all, I'm fairly new to .NET and I haven't done much in C++ before,
nothing complex anyway, but I have a pretty good understanding of
programming in general.

What I'm trying to do is create a .DLL that contains a lot of the functions
and classes that I normally use. I've followed the examples from
http://www.c-sharpcorner.com/2/pr12.asp which contains a pretty good example
of creating a simple .DLL. Even though the example is in C#, it was pretty
easy to translate into C++ .NET.

And here's the question : I was able to create a simple DLL which contains
classes, but I have not figured out how to include simple functions. Is it
possible to have functions in a DLL that aren't associated with any
particular class or object? "Simple" functions, as I call them?

Thanks,
/\/\ark
 
They're called global functions in c++
they're different than static.

http://c.ittoolbox.com/documents/document.asp?i=1596

If you were to reference this dll in .net, I doubt a global or static
methods will show up though, I did create a project a few months back in
managed c++ that used atl and when i referenced it in .net, it listed
structures that were not contained in a namespace, but those were structures
and not methods.
 
Thanks for the reply. I'm only somewhat familiar with the "static" keyword.
I understand that if I declare a VARIABLE static, there will only be one
instance for a library. So it could be used for keeping an employee count,
for example. But that's static variables, and I'm worried about static
functions or methods...

Are there any "gotcha's" when dealing with static methods? Are variables
used inside the static method automatically static as well? Does that cause
problems if two programs happen to use the same method out of the DLL at the
same time?

Thanks,
/\/\ark
 
You can think of as a static method member of a type as being the same as a
global member but scoped in the name of the type. That means that for e.g.
threading they behave identically as global functions would.

There are some really corner issues that make a difference but for the
mainstream use you can ignore all of that and almost think of these as
namespace scoped global methods.

Ronald
 
Hello Ronald, thanks for the reply.

Unfortunately, I don't have any experience with global functions in C++ so I
don't really comprehend what you are saying.

My concern is that I don't know if variables inside a static method are also
static by default? Because I am writing a DLL which contains functions that
could be called at any time... What happens if two programs, running at the
same time, call the same function from the DLL? Will the data be corrupted?
Or can the same static function be called twice at the same time without
interference?

The reason I am questioning this is that we have several dual CPU machines,
and some of the programs I'm writing take several minutes to complete.
These programs will call Static Methods in the DLL I am currently writing.
These Static Methods are meant as individual functions and should not
"share" any variables. So I don't know if I can actually use Static Methods
in this case?

Does this make sense?

Thanks,
/\/\ark
 
Mark said:
My concern is that I don't know if variables inside a static method are
also static by default? Because I am writing a DLL which contains
functions that could be called at any time... What happens if two
programs, running at the same time, call the same function from the DLL?
Will the data be corrupted? Or can the same static function be called
twice at the same time without interference?

Normally, variables introduced inside static functions are local to the
function invocation (they are on the specific thread's execution stack).
Thus they do not need to be protected by synchronization devices in
multi-threaded programs.

If a local variable is introduced with the 'static' keyword, then it is
shared between all invocations of that function. It is subject to race
conditions in a multi-threaded program.

It is very unlikely that you would want to introduce a static variable
inside a static function unless you really understand what you are doing and
have experience with multithreaded programming.
The reason I am questioning this is that we have several dual CPU
machines, and some of the programs I'm writing take several minutes to
complete. These programs will call Static Methods in the DLL I am
currently writing. These Static Methods are meant as individual functions
and should not "share" any variables. So I don't know if I can actually
use Static Methods in this case?

It sounds like you're fine.
 
Back
Top