Can I load a third-party .dll in R? - c

I am running Windows 7, and I know the DLL was built using Visual Studio C++ 2010. Since I don't have the source code I can't build it on-the-fly in R when creating a package (NAMESPACE). At this stage, I don't need to create a package, I just want to load the library in R using dyn.load(). Since it is a dll I suppose it is portable to any client program, but I would just make sure, because I know that it was not compiled using GNU gcc.
EDIT:
I want to call all the functions that the dll deploys.

Yes, R can load it using dyn.load. You may or may not be able to actually call the functions it exports, though. Unless the functions' arguments correspond to what R can handle, they won't be usable. If this is the case, you can write a wrapper dll that acts as a translation layer between it and R.

Related

Why a single line of code without dependencies also generates a larger dll [duplicate]

How exactly do DLL files work? There seems to be an awful lot of them, but I don't know what they are or how they work.
So, what's the deal with them?
What is a DLL?
Dynamic Link Libraries (DLL)s are like EXEs but they are not directly executable. They are similar to .so files in Linux/Unix. That is to say, DLLs are MS's implementation of shared libraries.
DLLs are so much like an EXE that the file format itself is the same. Both EXE and DLLs are based on the Portable Executable (PE) file format. DLLs can also contain COM components and .NET libraries.
What does a DLL contain?
A DLL contains functions, classes, variables, UIs and resources (such as icons, images, files, ...) that an EXE, or other DLL uses.
Types of libraries:
On virtually all operating systems, there are 2 types of libraries. Static libraries and dynamic libraries. In windows the file extensions are as follows: Static libraries (.lib) and dynamic libraries (.dll). The main difference is that static libraries are linked to the executable at compile time; whereas dynamic linked libraries are not linked until run-time.
More on static and dynamic libraries:
You don't normally see static libraries though on your computer, because a static library is embedded directly inside of a module (EXE or DLL). A dynamic library is a stand-alone file.
A DLL can be changed at any time and is only loaded at runtime when an EXE explicitly loads the DLL. A static library cannot be changed once it is compiled within the EXE.
A DLL can be updated individually without updating the EXE itself.
Loading a DLL:
A program loads a DLL at startup, via the Win32 API LoadLibrary, or when it is a dependency of another DLL. A program uses the GetProcAddress to load a function or LoadResource to load a resource.
Further reading:
Please check MSDN or Wikipedia for further reading. Also the sources of this answer.
What is a DLL?
DLL files are binary files that can contain executable code and resources like images, etc. Unlike applications, these cannot be directly executed, but an application will load them as and when they are required (or all at once during startup).
Are they important?
Most applications will load the DLL files they require at startup. If any of these are not found the system will not be able to start the process at all.
DLL files might require other DLL files
In the same way that an application requires a DLL file, a DLL file might be dependent on other DLL files itself. If one of these DLL files in the chain of dependency is not found, the application will not load. This is debugged easily using any dependency walker tools, like Dependency Walker.
There are so many of them in the system folders
Most of the system functionality is exposed to a user program in the form of DLL files as they are a standard form of sharing code / resources. Each functionality is kept separately in different DLL files so that only the required DLL files will be loaded and thus reduce the memory constraints on the system.
Installed applications also use DLL files
DLL files also becomes a form of separating functionalities physically as explained above. Good applications also try to not load the DLL files until they are absolutely required, which reduces the memory requirements. This too causes applications to ship with a lot of DLL files.
DLL Hell
However, at times system upgrades often breaks other programs when there is a version mismatch between the shared DLL files and the program that requires them. System checkpoints and DLL cache, etc. have been the initiatives from M$ to solve this problem. The .NET platform might not face this issue at all.
How do we know what's inside a DLL file?
You have to use an external tool like DUMPBIN or Dependency Walker which will not only show what publicly visible functions (known as exports) are contained inside the DLL files and also what other DLL files it requires and which exports from those DLL files this DLL file is dependent upon.
How do we create / use them?
Refer the programming documentation from your vendor. For C++, refer to LoadLibrary in MSDN.
Let’s say you are making an executable that uses some functions found in a library.
If the library you are using is static, the linker will copy the object code for these functions directly from the library and insert them into the executable.
Now if this executable is run it has every thing it needs, so the executable loader just loads it into memory and runs it.
If the library is dynamic the linker will not insert object code but rather it will insert a stub which basically says this function is located in this DLL at this location.
Now if this executable is run, bits of the executable are missing (i.e the stubs) so the loader goes through the executable fixing up the missing stubs. Only after all the stubs have been resolved will the executable be allowed to run.
To see this in action delete or rename the DLL and watch how the loader will report a missing DLL error when you try to run the executable.
Hence the name Dynamic Link Library, parts of the linking process is being done dynamically at run time by the executable loader.
One a final note, if you don't link to the DLL then no stubs will be inserted by the linker, but Windows still provides the GetProcAddress API that allows you to load an execute the DLL function entry point long after the executable has started.
DLLs (dynamic link libraries) and SLs (shared libraries, equivalent under UNIX) are just libraries of executable code which can be dynamically linked into an executable at load time.
Static libraries are inserted into an executable at compile time and are fixed from that point. They increase the size of the executable and cannot be shared.
Dynamic libraries have the following advantages:
1/ They are loaded at run time rather than compile time so they can be updated independently of the executable (all those fancy windows and dialog boxes you see in Windows come from DLLs so the look-and-feel of your application can change without you having to rewrite it).
2/ Because they're independent, the code can be shared across multiple executables - this saves memory since, if you're running 100 apps with a single DLL, there may only be one copy of the DLL in memory.
Their main disadvantage is advantage #1 - having DLLs change independent your application may cause your application to stop working or start behaving in a bizarre manner. DLL versioning tend not to be managed very well under Windows and this leads to the quaintly-named "DLL Hell".
DLL files contain an Export Table which is a list of symbols which can be looked up by the calling program. The symbols are typically functions with the C calling convention (__stcall). The export table also contains the address of the function.
With this information, the calling program can then call the functions within the DLL even though it did not have access to the DLL at compile time.
Introducing Dynamic Link Libraries has some more information.
http://support.microsoft.com/kb/815065
A DLL is a library that contains code
and data that can be used by more than
one program at the same time. For
example, in Windows operating systems,
the Comdlg32 DLL performs common
dialog box related functions.
Therefore, each program can use the
functionality that is contained in
this DLL to implement an Open dialog
box. This helps promote code reuse and
efficient memory usage.
By using a DLL, a program can be
modularized into separate components.
For example, an accounting program may
be sold by module. Each module can be
loaded into the main program at run
time if that module is installed.
Because the modules are separate, the
load time of the program is faster,
and a module is only loaded when that
functionality is requested.
Additionally, updates are easier to
apply to each module without affecting
other parts of the program. For
example, you may have a payroll
program, and the tax rates change each
year. When these changes are isolated
to a DLL, you can apply an update
without needing to build or install
the whole program again.
http://en.wikipedia.org/wiki/Dynamic-link_library
DLL is a File Extension & Known As “dynamic link library” file format used for holding multiple codes and procedures for Windows programs. Software & Games runs on the bases of DLL Files; DLL files was created so that multiple applications could use their information at the same time.
IF you want to get more information about DLL Files or facing any error read the following post.
https://www.bouncegeek.com/fix-dll-errors-windows-586985/
DLLs (Dynamic Link Libraries) contain resources used by one or more applications or services. They can contain classes, icons, strings, objects, interfaces, and pretty much anything a developer would need to store except a UI.
According to Microsoft
(DLL) Dynamic link libraries are files that contain data, code, or resources needed for the running of applications. These are files that are created by the windows ecosystem and can be shared between two or more applications.
When a program or software runs on Windows, much of how the application works depends on the DLL files of the program. For instance, if a particular application had several modules, then how each module interacts with each other is determined by the Windows DLL files.
If you want detailed explanation, check these useful resources
What are dll files , About Dll files

Creating Backwards Compatible Drop In Statically Linked DLL

I have a C-based DLL that I wrote years ago for a project and it exports a set of functions that define an API. Now I need to re-write this DLL's internals but keep the API exactly the same.
The user of the DLL used static linking and they do not want to or are unable to recompile their executable.
I've noticed that the RVAs of the exported functions are different. My understanding is that means the executable won't be able to find the functions unless it is re-linked with the updated lib file.
Is there a way in VS2017 to force an exported function to use a specific RVA? I checked the Microsoft LINK DEF file format and I didn't see an option in there.
Even if it is possible, is fixing the RVAs enough to ensure the old executable will be able to use the updated DLL or are there additional complications that make this a non-starter?
Thanks.
When you statically link an EXE module against a DLL, you do indeed link against the the DLL's import library (a .LIB) created alongside the DLL when the DLL was built. This is not the same thing as linking against a static library which is confusing because those are also .LIB files.
The first thing you should do is figure out if your EXE module has an import entry for said DLL using a tool like Dependency Walker, Dumpbin, pelook or your favorite PE analyzer tool. If there is no DLL import entry, you have have probably linked the EXE against a static library as described by #HAL9000 's answer. Short of reverse-engineering the EXE, your best bet would be to rebuild the module as suggested if possible.
Otherwise, if you do find an import for said DLL, then yes you can swap out a newly-built DLL provided you have the same export (function) names and/or ordinal values as the original. DLLs find function by export names and/or ordinal values, not RVAs which in this case are only an internal detail. Whether the DLL is implicitly loaded (from being statically-linked) during process (EXE) initialization (before the EXE's entry point is called) or explicitly loaded (via code using LoadLibrary, etc.) the whole point of being a DLL is that it is a module is designed to be dynamically replaced and Windows was designed around this concept. The internal RVAs both within the EXE (referencing the DLL) and the DLL itself do not need to match an old DLL's values; this bookkeeping is automatically handled by the Windows loader during a process also known as runtime linking.
In the event the EXE is linked against said DLL and ALSO specifies hard-coded addresses (RVAs) for the DLL's exported functions (a process known as static binding), Windows will still verify the addresses still internally reflect the correct values in the DLL that is actually loaded which may be a different, updated DLL. This is done via a timestamp check in the import section for the DLL. If there is a mismatch, the Windows loader tosses-out all of the static RVAs and updates them with the current values incurring a slight performance penalty, but the program will still load. FWIW the bind.exe tool to do this static binding no longer ships with the Visual C++ toolset as the performance gain in modern versions of Windows is minimal. This optimization used to be be common practice to speed up load times, especially in OS-supplied system DLLs, but shouldn't affect what you are trying to do one way or the other.
If the user has statically linked in your library, then it is not a DLL, and making a drop in replacement without relinking is not possible. At least not without some ugly hacks. The old library functions have been copied into the executable, so there are no way around editing the executable. If you can't recompile or relink, then it is probably easier to rewrite the executable from scratch.
Mucking around with adresses of functions in your new DLL, if possible, can't have any effect if the executable doesn't have any code to load a DLL at all.

Compile Fortran program with IFORT and using object files (i.e. .lib) compiled in Microsoft Visual Studio

I have a basic question that I cannot find the answer to even after searching the web many times.
Is it possible to compile a Fortran program by IFORT that uses (as dependencies) object files (i.e. .lib) that were compiled by Microsoft Visual Studio C?
Yes, it is possible, and is even rather easy. You have several options for how to do this:
Add the MSVC library project to your Fortran Visual Studio solution and then use Project > Dependencies to add the C project as a dependent of your Fortran project *does not work if the C project creates a DLL)
Add the .lib from the C project to the Fortran project as if it were a source file
Name the C .lib in the Linker > Input > Additional Dependencies project property.
I generally recommend the first choice, as it means you don't have to fuss with different settings for Debug and Release projects. You do need to make sure that the C library is built to specify the same run-time library type (Debug vs. Nondebug, DLL vs. Static) as the Fortran project. This is in the Code Generation property page for C.
There is a worked example "Fortran Calls C" in the Intel Parallel Studio XE for Windows Sample Bundle.
You'll also need to understand how to call a C procedure from Fortran and ensure that the C arguments have compatible Fortran types. It works best if you make use of the Fortran standard's "C interoperability" features.

Different results with same inputs but different ways of inputting

I have an external function from an opencv dll, cvCreateFileCapture, that should take the path as an input and return a structure.
CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
I created a vs2010 project for testing purposes from where i call this function with a valid file path and get the structure i was supposed to get returned.
When I call this function from a different program (I'm using labVIEW) I jump into the same function with the same input but it returns 0.
Does anyone have an idea why it works 1 way but not the other?
The C call is CvCapture* p = cvCreateFileCapture("C:/Users/****/Downloads/Disturbedloc.avi");
the labview call looks like this:
Load time debugging
If the library call works in one environment but not another, then there's a difference in the environment. There are two main approaches:
Use depends.exe to make sure that opencv has the correct linkage. Perhaps Visual Studio is inspecting opencv for other libraries (like FFMPEG or GStreamer) and adding extra loader instructions to fetch those binaries on launch. LabVIEW, on the other hand, will do a pure dynamic load, and if the library doesn't correctly advertise its dependencies, then those libraries won't be loaded and you'll get a NULL pointer.
Attach Visual Studio to LabVIEW before your VI makes the first call into opencv then watch the Modules window to see if the support libraries are resident in memory. Comparing against your working Visual Studio program would show if any are missing.
Loading missing libraries by force
That run-time linking doesn't work in opencv is a bug in their project. You should report that to them.
You have two workarounds:
Fix the bug, build opencv yourself, and redistribute it with your application.
Call dummy entry points in each dependency to prompt LabVIEW to bring them into the process. Then start calling into opencv.
Once it's working
Since your opencv dll returns a pointer to a struct, you have two options:
Wrap and adapt the opencv library in another C/C++ library so that the inputs/outputs are simpler data types, like numerics.
Use a cluster and some specific Call Library Function node configuration so that LabVIEW can understand the memory layout of the opencv types. NI has a some good documents describing how LabVIEW interfaces with external libraries, but start here: Calling C/C++ DLLs from LabVIEW.
I've just tried and it is working for me.
Here is the front panel:
And the block diagram:
I'm using Labview 2014 and OpenCV 3.0.
I had problems (i.e. 0 returned) with more complex Paths (including spaces, etc.)

Why do I need a redistributable package on unmanaged code? (msvcp100.dll)

What is the purpose of the 'msvcrXXX.dll' and 'msvcpXXX.dll'? And what are the differences between msvcr and msvcp?
Why do I need to link them to my binary if it's just a very simple and unmanaged .dll? Is it because it's part of the linker? Why isn't it in the Windows system directory by default as a common library?
I'm just trying to understand why in Windows there are somethings so complicated....
msvcrXXXX.dll is the DLL for the C runtime library.
msvcpXXXX.dll is the DLL for the C++ runtime library.
One or both of these dependencies will be added to your binary if you are building using /MD or /MDd, which are the defaults specified by Visual Studio when you create a new C++ project. Using either of these flags indicates that you want your program to link with the DLL version of the C/C++ runtime. You can change the defaults under Project Properties->Configuration Properties->C/C++/Code Generation/Runtime Library.
If you change your project to use /MT or /MTd, then your application will not generate references to either of the DLLs listed above, because the C/C++ runtimes will be linked directly into your program. For most simple programs, this will not cause any problems. However, if your program is broken up into several DLLs that all are built using these flags, then each DLL will maintain a copy of the referenced CRT functions and static data, and you may run into memory allocation/freeing problems. To avoid these, you need to make sure that objects allocated within a given DLL are also freed in that same module.
In general, it is more efficient to use the /MD and /MDd flags for applications that have multiple modules (DLLs), because all of those modules will share a single copy of the C/C++ runtime libraries and their associated data structures within the application process.
For simple, single-module applications, though, feel free to build using /MT or /MTd.

Resources