Link to home
Start Free TrialLog in
Avatar of npl77
npl77

asked on

Calling a Win DLL from C#

I am trying to call an LDAP function from the WLDAP32 dll (which I cannot add a reference to since its umanaged code). Can someone tell me how to do this. I think I have to match the parameters to an equivalent in C# here is what the C++ function looks like:

ULONG ldap_simple_bind(
  __in  LDAP* ld,
  __in  PCHAR dn,
  __in  PCHAR passwd
);

So how can I use the dll to call this function and use it in my C# application??
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of npl77
npl77

ASKER

here is a code snippet in C++ can you recreate the code in C#
int main(int argc, char* argv[])
{
	// LDAP session handler.
	LDAP 	*p_ld=NULL;
 
	int intRC = 0;
 
	// Get server name/ip from user.
	char server[30];
	printf("LDAP Server:");
	scanf("%s", server);
 
	// Get server port from user.
	int port;
	printf("Port #:");
	scanf("%d", &port);
 
	printf("Try to connect to %s on port number %d...\n", server, port);
 
	// Connecting to server.
	if ((p_ld = ldap_open(server, port)) == NULL)
	{
		printf("Failed to open connection to LDAP server.\n");
	}
	else
	{
		printf("Connected to LDAP server.\n");
		
		// Search result handler
		LDAPMessage *p_ldResults=NULL;
 
		// Get search base from user.
		char searchbase[256];
		printf("Search Base: ");
		scanf("%s", searchbase);
 
		// Get search filter from user.
		char filter[256];
		//printf("Search filter: ");
		//scanf("%s", filter);
 
		char userid[30];
		printf("User ID: ");
		scanf("%s", userid);
 
		char userpwd[35];
		printf("Password: ");
		scanf("%s", userpwd);
 
		sprintf(filter, "(uid=%s)", userid);
 
		// Search
		intRC = ldap_search_s(
								p_ld,
								searchbase,
								LDAP_SCOPE_SUBTREE,
								filter,
								NULL,
								0,
								&p_ldResults
							);
 
		// Retrive the results
		if (intRC == LDAP_SUCCESS)
		{
			if (p_ldResults == NULL)
			{
				printf("No results found.\n");
			}
			else
			{
				// Find out the number of entries in the result.
				int intCount = (int)ldap_count_entries(p_ld, p_ldResults);
 
				// Make sure we found 1 and only one.
				if (intCount > 1)
				{
					printf("Too many matching entry.\n");
				}
				else
				{
					if (intCount == 1)
					{
						// Try to get the user dn.
						LDAPMessage *p_ldEntry=NULL;	
						char *p_dn=NULL;
 
						p_ldEntry = ldap_first_entry(p_ld, p_ldResults);
 
						if (p_ldEntry != NULL)
						{
							p_dn = ldap_get_dn(p_ld, p_ldEntry);
 
							if (p_dn != NULL)
							{
								// Authenticating user.
								printf("Authenticating user...\n");
 
								intRC = ldap_simple_bind_s(p_ld, p_dn, userpwd);
 
								if (intRC == LDAP_SUCCESS)
									printf("Successful\n");
								else
									printf("Failed\n");
 
								// Must do this.
								ldap_memfree(p_dn);
							}
							else
							{
								printf("Failed to retrieve user DN.\n");
							}
 
						}
						else
						{
							printf("Failed to retrieve user entry.\n");
						}
					}
					else
					{
						printf("Cannot find user.\n");
					}
				}
			}
		}
		else
		{
			printf("Failed to execute search operation. Error code %d\n", intRC);
		}
 
		// Must do this.
		if (p_ldResults)
			ldap_msgfree(p_ldResults);
								
	}
 
	// Close the connection.
	ldap_unbind(p_ld);	
 
	return 0;
}

Open in new window