Declare and <DllImport> in VB.NET have different results - dllimport

I've been trying to call a login method of an unmanaged DLL.
If I use Declare the login fails.
Private Declare Function Login Lib "dllCore" (ByVal lpName As String, ByVal lpPassword As String) As Int32
Login ("Steve", "123456") ' THIS FAILS TO LOGIN ALTHOUGH THE PARAMS ARE CORRECT
If I use DllImport, it works !!
<DllImport("dllCore.dll",
EntryPoint:="Login",
SetLastError:=True,
CharSet:=CharSet.Unicode,
ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)>
Private Function Login(ByVal username As String, ByVal password As String) As Integer
End Function
Login ("Steve", "123456") ' NOW WORKS
Has anyone any ideas why I get this behaviour ??

The default character set for a Declare statement is Ansi. You need to set the charset to Unicode to properly match the DllImport.
Private Declare Unicode Function Login Lib "dllCore" (ByVal lpName As String, ByVal lpPassword As String) As Int32
MSDN documentation for the Declare statement

Related

Call DLL function with alias "MAX" in Excel

I have a function in my DLL with alias "MAX".
Then I try to use it via VBA in my Excel document:
part of VBA-file:
Declare PtrSafe Function MAX_impl Lib "model_64.dll" Alias "MAX" (x As Variant, ByVal idx_f As Integer, out As Variant) As Integer
cell value in Excel-file:
=MAX(B2:B3, 1)
But instead of call to my DLL-function I got a call to MAX function from Excel.
Is there a way to specify that call should be performed to DLL?
Maybe some prefix is possible?
Like:
=<prefix>.MAX(B2:B3, 1) # call to DLL function
Solution which worked for me: I ended up with this prefix - Module1
So in Excel-file I writes:
=Module1.MAX(B2:B3, 1) # this actually calls my DLL function
From the top of my head, I'd suggest you create a public function in a VBA-Module and then call this from within the Excel-Cell.
Say, you want to Call =MyMax(...) in order to use the function from your DLL. Then, you'd need to follow those two simple steps:
Add a module (if not already present), by using Insert ➡️ Module
Within this module, please create a public function like so:
Public Function MyMax(x As Variant, ByVal idx_f As Integer, out As Variant) As Integer
MyMax = Max(x, idx_f)
End Function
This allows you to create function MyMax that you'll be able to call from your spreadsheet. This, in turn, just acts as proxy to call the actual function from the DLL and returns it's value to the cell.
Note: I've not tested it, but I suppose you're better of aliasing your function with a name i) other than "Max" to avoid confusion with built-in functions and ii) other than your proxy function to avoid confusion with that one, as well.
More detailed instructions on how to create such a user defined function, can be found here.

How to obtain the typed field instances from a typed org.jooq.Table instance

I have a function that returns an org.jooq.Table<RecordN<.....>> is there a way to obtain typed field instances without having to specify the types again?
final Table<Record11<String, Long, String, String, String, String, String, Timestamp,
String, String, Timestamp>> upcoming =
tableUpcomingFor(i_app, i_req, sport_idName, minDateName);
At the moment I have to do something like:
final Field<Timestamp> minDate = upcoming.field(name("minDate"), Timestamp.class);
This seems redundant, I should be able to get the typed fields by position. Is there a way to do that?
I require multiple fields for use in an outer query that uses the table instance.
Any ideas how to do that?

Register Excel UDF without arguments

I has many user define function with arguments and without. I use CUdfHelper from this article http://www.jkp-ads.com/articles/RegisterUDF00.asp for register function.
Registered function ask arguments for arguments, even if they are not.
Example my fuction without arguments:
Public Function getProjects()
getProjects = Utils.execute("getProjects", "getWSEntitiesData")
End Function
On MyFunction.c
#include <windows.h>
#define DLL_EXPORT __declspec(dllexport)
DLL_EXPORT void getProjects() {
return;
}
compile on MyFunction.dll
I register the function with these parameters.
SetGlobalName = Application.ExecuteExcel4Macro("MyFunction.dll", "getProjets", "P", "getProjects", "", 1, "MyFunctionCategory", "", "", "Return list projects")
If I register as
SetGlobalName = Application.ExecuteExcel4Macro("MyFunction.dll", "getProjets", "P", "getProjects",, 1, "MyFunctionCategory", "", "", "Return list projects")
Function argument dialog is displayed all the same.
If I register as
SetGlobalName = Application.ExecuteExcel4Macro("MyFunction.dll", "getProjets", "P", "getProjects",, 1, "MyFunctionCategory")
Function argument dialog isn't displayed, but the description is no longer available.
REGISTER() Arguments
Path and name of the dll
Name of the function you wish to call
Type string
The name you want to use in Excel cells
A list of arguments to use in the function wizard
The Macro type (2 for a function, 2 for a command)
Which function wizard category to add the function to
Short cut text if the function being registered is a command
Path to help file
Function help to show in the function wizard
11-30 onwards help text for each argument in the function wizard.
I think the problem is in the arguments, as by default it is set to an empty string, and I can not figure out how to change the parameters to the function.
On CUdfHelper
' structure definition
Private Type REGARG
sDllName As String
sDllProc As String
sArgType As String
sFunText As String
**sArgText As String**
iMacType As Integer
vCatName As Variant
sKeyText As String
sHlpPath As String
**sFunHelp As String**
aArgHelp(1 To 20) As String
End Type
How to correctly set the parameters so that the window does not appear, and stores the description?
If register function with ExecuteExcel4Macro and set Function Description. In an XLL add-in, however, you may run into a bug in the Excel API. The bug shows itself if a parameterless function is mentioned in an ADXExcelFunctionDescriptor that has a non-empty string in the Description property. If this is the case, you'll get another version of the Function Arguments dialog.
That is, to bypass that issue, you need to leave the ADXExcelFunctionDescriptor.Description property empty.
Find on https://www.add-in-express.com/docs/net-excel-udf-tips.php

Cannot create automation object - lotusscript

I have a dll which is registered in GAC and declared in my form. I am using Notes 9 and windows 7. The declaration -
Declare Function CreateAccount Lib
"c:\Program Files (x86)\PwC\SDCADInstall\SDCADLib.dll" (sADServer As
String , sUserID As String ,sPassword As String , sRoot As String,
sLocation As String , sLoS As String , sSBU As String , sFName As
String, sLName As String , sADId As String , sDescription As String
, sOffice As String ) As String
I am getting "Cannot create automation object" error while creating the object. The parameter values are collected by another function. Pl find the code below :
Dim obj As Variant
enter code here
Set obj = CreateObject("SDCADLib.Account")
retval = obj.CreateAccount(sADServer , sUserID ,sPassword , sRoot, sLocation , sLoS , sSBU , sFName, sLName , sADId , sDescription , sOffice )
CreateADAccount = retval
I am stuck here for ages. Please help.
I'm a bit confused because you include a Lib declaration, which is not necessary if you are using CreateObject to access your DLL from LotusScript via COM.
Since you mention the GAC, I presume you have written your DLL as a .NET assembly. Have you seen this IBM Technote about calling .NET classes from LotusScript and followed the instructions there? Specifically, have you followed the instruction that tells you that you have to run regasm in order to publish your DLL via COM. Without that, CreateObject won't find it.

Unable to add custom properties to an Excel Workbook

Excel workbooks have a CustomDocumentProperties collection. This collection is of type DocumentProperties and has an Add method that I'm attempting to call from MATLAB.
If I call the invoke method on the collection I see:
>> workbook.CustomDocumentProperties.invoke
Item = handle Item(handle, Variant, int32)
Add = handle Add(handle, string, bool, int32, Variant(Optional))
I assume this means the Add method requires a string, boolean, int32, and an optional variant, and this matches with the Microsoft documentation for the Add method.
However, all combination of inputs I've tried to this function result in an error. For example:
workbook.CustomDocumentProperties.Add('MyProp', true, int32(1), true);
Results in the error:
Invoke Error: Incorrect number of arguments
If I supply 7 or more arguments then I get the error:
Error: Invalid number is arguments. This method can take maximum 6 arguments
If I supply anything other than a string as the first argument I get the error:
No method 'Add' with matching signature found for class 'Interface.2DF8D04D_5BFA_101B_BDE5_00AA0044DE52'.
Has anyone successfully used this function to add a custom property to an Excel workbook from MATLAB?

Resources