Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the C++ part #83

Closed
haller9816 opened this issue Aug 29, 2018 · 6 comments
Closed

the C++ part #83

haller9816 opened this issue Aug 29, 2018 · 6 comments
Labels

Comments

@haller9816
Copy link

haller9816 commented Aug 29, 2018

  • Environment ( VS, MSBuild, ...): Visual Studio 2017

Hi Denis, hi all,

it's me again. I have the VB.NET code with DLLEXPORT with no ERROR.
dllexport_visualbasic

Now I try the C++ calling as a console application (later it should be a dll) and have problems:
dllexport_cpp_part
errors are: GetProcAddress, HMODULE, lib and LoadLibrary are not found or declared (looks if I have something to load)
and I think the declaration:
extern "C" _declspec(dllexport) double cNetz(double p);
is not complete.
I have no experience with C or C++, I do programm in FORTRAN. That was enough for technical calculations.
Would you help me again?

Olaf

@3F
Copy link
Owner

3F commented Aug 30, 2018

Looks like we need to add more info for beginners, but how does it related ... hmmm -_-

Also, as I understood, my two latest screencasts (this, or this) that combines a several methods at once (for both host sides) completely confuses some people. Some people think that this is only about exporting from .NET (this dllexport), others combines unnecessary code, and so on. Well, detailed chapters with description seems does not help.

Olaf, your 6 line code in details:

  • You don't need underscore character for lpProcName arg if you have no this proc:
GetProcAddress(
    _In_ HMODULE hModule,
    _In_ LPCSTR lpProcName   // <<< You have "Netz" exported function, thus you need this and not "_Netz"
    );
  • By the way, __declspec with double underscore "__" is a C++ standard and it's more recommended, at least outside of MSVC compiler. Same for other keywords like for calling conventions: __cdecl, __stdcall, ... but except for data types like __int32, __int64, ... (MS features).

    • You can also avoid __cdecl in double(__cdecl *Netz) if your projects uses this convention by default. That is: double(*Netz)(double p)
  • But for your case, you do not need this part at all:

extern "C" __declspec(dllexport) double cNetz(double p)

This was for .NET to use unmanaged library, if you remember, like:

using(var l = new ConariL("unmanaged_c_library.dll"))
{
    Double val = l.DLR.cNetz<Double>(1234); // __declspec(dllexport) double cNetz(double p)
  • Please be careful, managed and unmanaged types are not identical, and also their sizes may not be equal!
    Moreover in C and C++ lot of things can be redefined even for std types like #define double __int64. So, You should always look for equivalent for bytes. Check this via sizeof: sizeof(double)
    Default types here: https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges

  • LoadLibrary() & GetProcAddress() are parts of Win API, you need windows.h. Add the following headers into your stdafx.h (because you're using precompiled headers as I can see):

// Win API including +LoadLibrary/Ex and lot of other related things
#include <windows.h>

// if you want to work with TCHAR like from my screencasts:
// otherwise you can via wchar_t or char, etc.
#include <tchar.h>
  • Try do not use typedef inside any functions or methods. From screencasts it was for quick illustration of the main steps. Header files are better place.

@3F 3F added the question label Aug 30, 2018
@haller9816
Copy link
Author

haller9816 commented Aug 30, 2018

Hi Denis, hi all,

since your comment I changed something:
I delete the underscors
I adde
#include <Windows.h> in stdafx.h and
delete "extern "C" __declspec(dllexport) double cNetz(double p)"

That gives a compiling without errors

but if I run the program I get an exception error when the function is called.
dllexport_cpp_part_02

May be the parameter list is not ok or the types are not ok, how you write. (I attached a Dependency Walker printout from the vb.net dll. Shoudn't be there "Netz@8"?)
dep walk_vb_dll

I don't know what to do. And I must confess I didn't understand all of the comment. May be I considered not all.
Please..... help again.

Olaf - the C++ beginner :-)

@3F
Copy link
Owner

3F commented Aug 30, 2018

Remove #define double __int64 This note was about possible different sizes! For your future work with data, because it can be fatal for some calculation isn't it? So you don't need this line too.

Shoudn't be there "Netz@8"

There is some difference from msvc compiler, but even for msvc if 64bit environment, no, and for 32bit env with __cdecl also no. Here I meant C compiler or C++ with C-mangling.

Some other compilers does this also differently: MSVC, GCC, CLang, ...

Olaf - the C++ beginner :-)

Why you don't what to use Fortran if you already know this perfectly?!

About Unhandled exception (KernelBase.dll):

More like the export works well, and you just have problems in your VB function :) Check this out.

Try to debug in mixed code. Probably will help to isolate problem.

@haller9816
Copy link
Author

haller9816 commented Aug 30, 2018

Hi Denis,

I would do it in Fortran, but I don't know how. But I will try.
And about the problems in the VB function, I will use the mixed code debugging.

Thank you for your help.

Olaf

@haller9816
Copy link
Author

Hi Denis,

excuse me once again very, very much,
But I can not step into the vb. I made a project (C++), added the other (VB ) and did a reference in the C++-console to the VB-classlibrary.
And while debugging the same error like before occurs. Looks like the interface dosn't work or my mixed language work not correct.

Would you help me again?

Olaf

ps: the code is the same like before

@3F
Copy link
Owner

3F commented Aug 31, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants